Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 MD_Reptile · Apr 03, 2012 at 12:25 PM · rigidbodyvector3quaternionvelocitylookat

2d game - looking toward velocity for rigidbody player

Ok I am having a problem with rotating the Z axis of my player to face towards his movement.

Here is my code - note that my object is oriented with Vector3.up being his "front":

 desiredAngle = Vector3.Angle(Vector3.up, rigidbody.velocity);
         if (dir != Vector3.zero)
         {
             if(dir.x > 0.1)
             {
                 desiredAngle = -desiredAngle;
             }
             transform.eulerAngles = new Vector3(0, 0, desiredAngle);
         }

Now this works properly as far as the object facing as it should, problem is that when the player faces "negative desiredAngle" then the rigidbody "jerks" for only about 1 frame as it starts to turn to the right. This is ugly, and has got to have a better solution. Maybe a different way to calculate the angle, that wraps all the way around to 360 degrees, instead of just calculating the closest angle to a vector.

Or maybe I could do some smoothing somehow that would make the transition between a negative value for desiredAngle, and positive value, not noticeable? Rather than really twitchy when it switches over

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
Best Answer

Answer by MD_Reptile · Apr 03, 2012 at 07:31 PM

OK let me start by saying I had to dig through the internet for DAYS to find this dang solution! but I did manage to solve my problem, by converting my vector 3 (my rigidbody.velocity) into a vector2 (the x and y of rigidbody.velocity), then convert that vec2 into a radian, then convert that radian into a degree, and finally, add 90 degree offset to allow my player object to exactly follow his velocity with only rotating his Z axis. Here is the code I used for those future question'ers who will certainly stumble all over unity answers trying to find this:

http://answers.unity3d.com/questions/189870/convert-vector2-mouse-position-into-angle.html

a million thanks to the guy who supplied this...only changes I needed to make were to use rad2deg rather than radtodeg like that says! fantastic! works flawlessly!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate player (rigidbody) towards his movement 2 Answers

How do I get one object to point toward another using rigidbody physics? 2 Answers

Rigidbody to follow player around 1 Answer

Comparing rigidbody speeds 1 Answer

Rigidbodies with fixed joint connection rotate instead of forward/back 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