Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jyumaidev · Sep 04, 2021 at 08:49 PM · quaternionrigidbody.velocitylookrotation

LookRotation not working as expected when turning GameObject in direction of Velocity

I created a script to always keep a character facing its rigidbody.velocity. Everything seems to work fine for the most part, until input stops and sometimes the character rotates to a seemingly random direction. The object is part of a hierarchy, however the parent object itself is not being rotated, only child objects. Therefore, this object should only be affected by the following code, which is called in Update:

 private void MatchRotationToVelocity()
     {
         Vector3 rotateTo = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z).normalized;
         if (rotateTo != Vector3.zero)
         {
             meshModel.rotation = Quaternion.Slerp(meshModel.rotation, Quaternion.LookRotation(rotateTo), rotationSmoothTime);
             lastFacingDirection = meshModel.rotation;
         }
         else
             meshModel.rotation = Quaternion.Slerp(meshModel.rotation, lastFacingDirection, rotationSmoothTime);
     }



The else statement shouldn't even be needed. The idea is that if there is no velocity, the object should maintain its current rotation. Any ideas why this isn't working as expected?

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 Bunny83 · Sep 05, 2021 at 08:13 AM

Well, your if statement doesn't really handle "no input" properly. First of all your velocity most likely is not completely 0 on all axis, unless the rigidbody actually starts sleeping. Before that the velocity may have a tiny rest offset. Since you normalize this vector, the vector will always have a length of 1 with the only exception when the velocity is really really small or equal (0,0,0) in which case "normalized" returns Vector.zero.


You should not normalize the velocity before your if statament. Also you probably want to have a minimum velocity threshold. Something like this:

      Vector3 rotateTo = rigidbody.velocity;
      rotateTo.y = 0f;
      if (rotateTo.sqrMagnitude > 0.01f)
      {
          meshModel.rotation = Quaternion.Slerp(meshModel.rotation, Quaternion.LookRotation(rotateTo), rotationSmoothTime);
      }

Note when using sqrMagnitude the threshold also need to be squared. So the actual min velocity limit is 0.1 when using 0.01


Keep in mind that LookRotation actually takes 2 arguments. When you don't provide an up vector, you will implicitly use Vector3.up as up vector.

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

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

Related Questions

Quaternion rotation issue 1 Answer

"Look rotation viewing vector is zero" error 2 Answers

LookRotation Between 2 Points 1 Answer

Rotating a 2D sprite to face a target on a single axis. 3 Answers

More specific Quaternion.LookRotation 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