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
1
Question by ms345 · Feb 17, 2014 at 11:12 AM · rotationquaternionrigidbody2dvelocity

Rotate Rigidbody 2D towards velocity

I'm attempting to use this code from a previous AskUnity question to rotate the nose (the right edge) of a rectangle(Rigidbody2d) towards the velocity it is currently travelling.

"transform.rotation=Quaternion.SetLookRotation(velocity);"

I've converted the current velocity of my Rigidbody from a vector2 to a vector 3, but because it rotates the X axis, and not the Z axis, as soon as it rotates in the 3D space the item disappears. How would i rotate the right edge towards the velocity it's travelling?

Thanks!

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
1

Answer by robertbu · Feb 17, 2014 at 01:27 PM

Here is a bit of code that will rotate the object to face the direction of the velocity over time:

 function Update () {
     var dir = rigidbody2D.velocity;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     var q = Quaternion.AngleAxis(angle, Vector3.forward);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotateSpeed * Time.deltaTime); 
  
 }

'rotateSpeed' is a variable you define and whose value is in degrees per second. If you want eased movement, replace 'RotateTowards' with 'Slerp' and adjust 'rotateSpeed' downwards a bunch.

Comment
Add comment · Show 2 · 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 ms345 · Feb 17, 2014 at 01:45 PM 0
Share

Really appreciate you taking the time to respond to this.

It works and the item points in roughly the right direction but it's very very twitchy even with the rotationSpeed slowed right down and with Slerp applied.

It's constantly vibrating and adjusting the rotateSpeed doesn't seem to affect it, ins$$anonymous$$d it changes the angle it's pointing at.

Any ideas how I can smooth out this twitch?

Thanks again.

avatar image robertbu · Feb 17, 2014 at 09:36 PM 0
Share

Is your velocity changing frame to frame? If so, maybe you want to average the velocity of a fixed number of frames. And you did say you tried Slerp() ins$$anonymous$$d of RotateTowards()? If averaging does not solve the problem, post a copy of your movement code and I'll play a bit.

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

18 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 avatar image

Related Questions

i have rotated an gameobject but the axis of the object did not change 2 Answers

2D Rotate towards GameObject acting odd. 2 Answers

2D Sprite/object rotation changing despite telling it not to 0 Answers

Unity C# 2D Adding Velocity on rotation 1 Answer

why can't i divide rb2D.velocity.x by rb2D.velocity.x? [Fixed] 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