• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronaldosilva · Mar 08, 2012 at 04:30 AM · rotationangleaim

The angle in forward axis between two points.

Hi, guys. I need your help. I have one game object and a point on space that I got from a Ray casted from the main camera. I tried very hard to make this object aim to that point just in the forward axis, like a Sidescroller shooter, where the mouse pointer is the aim of the gun (up and down only). I tryed everything but something is wrong.. please advise.

 void Update () {
     
     if(Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition),out hit, 100))
     {
         targetLook = hit.point;
         //now i need to make my object aim to the targetLook, but only on the forward axis.
         //I tryed using Atan2, that I usually do with Flash based games, but in 2D Space..
     }
 }

Appreciate any help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Mar 08, 2012 at 05:31 AM

What's wrong with Transform.LookAt? if the object's center and the point are on the correct plane (defined by the object's up vector and it's center, if I understand correctly), or said differently, if both object and point have y == 0, it should be ok.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronaldosilva · Mar 08, 2012 at 01:57 PM 0
Share

The problem is that the target point is obtained by the X and Y position of the mouse, and the Z position can vary on the depth that of the surface the Raycast hit returns to me..

I need to ignore the Z and only get the X and Z position. Thats why the LookAt does not work =/

avatar image Berenger · Mar 08, 2012 at 04:42 PM 0
Share

Ok. Then, you can use an empty gameObject with a box collider that covers all the floor (something like (50,1,50)) with a specific layer then you cast the ray only on that layer, that way the Y won't change.

You can use Plane.Raycast as well, which return a distance and the hit point is calculated by **ray.origine + ray.direction.normalized distance*.

Finally, you can use Vector3.Scale( Camera.main.ScreenToWorldPoint(mousepos), new Vector3(1,0,1) ). If the camera is orthographic.

avatar image ronaldosilva · Mar 08, 2012 at 10:27 PM 0
Share

Wow! Thank you! =)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Bone Rotation on X (or any) axis Issues 1 Answer

Rotating sprite through touch (storing current rotation) 0 Answers

How can I multiply the rotation of a mirrored object? 1 Answer

rotate slowly! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges