• 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 KillMobil · Dec 01, 2013 at 02:00 PM · 2dangle2d rotation

2D Angle Calculation

I have a character that bounces around and i want him to look always towards the direction he is going. so i used this code:

 var angle : float = Mathf.Atan2(myRigid.velocity.y, myRigid.velocity .x) * Mathf.Rad2Deg;
 myRigid.rotation.eulerAngles.z = angle - 90;

when applied the code to the object seems to be of by +/- 5 degrees.

in the picture below i the pink line is the velocity of game object.

any thoughts or ideas why this could be happening ?

Thanks in advance!!

alt text

screen shot 2013-12-01 at 3.01.57 pm.png (89.3 kB)
Comment
Add comment · Show 6
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 robertbu · Dec 01, 2013 at 03:39 PM 0
Share

No idea why this is happening, but you should not set a eulerAngles axis separably. From the manual:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

avatar image KillMobil · Dec 01, 2013 at 03:48 PM 0
Share

i just tried to create a new "Quaternion" and apply the rotation to the body with "$$anonymous$$oveRotation" but problem still there... is there another way to apply rotation?

avatar image robertbu · Dec 01, 2013 at 04:44 PM 0
Share

Despite my comment about not setting individual axes, you code should work most of the time, and when it doesn't work, it would be way off. I can make some alternate suggestions, but since I don't understand why you have the problem in the first place, they are only guesses:

 myRigid.transform.up = myRigid.velocity;

Or:

 myRigid.rotation = Quaternion.FromToRotation(myRigid.transform.up, myRigid.velocity) * myRigid.rotation;

Or:

 var angle : float = $$anonymous$$athf.Atan2(myRigid.velocity.y, myRigid.velocity .x) * $$anonymous$$athf.Rad2Deg;
 myRigid.rotation = Quaternion.AngleAxis(angle-90, Vector3.forward);
avatar image KillMobil · Dec 02, 2013 at 12:45 PM 0
Share

Thanks for the code robertbu!

i agree that the code should either work or have random results! especialy since i am getting the same problem from the other codes you send!

ill go through the script and probably rebuilt most of it! must be something else affecting it!

avatar image meat5000 ♦ · Dec 02, 2013 at 12:52 PM 0
Share

Have you considered LookAt function?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hand spinner script 1 Answer

Find the difference between the Z rotation of two GameObjects 2 Answers

Finding the angle between 2 clicked points 2 Answers

rotate 2d circle on z axis ? 1 Answer

How do i get a attack offset of the player at a fixed distance in 2D? 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