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 nesdroc · Feb 19, 2015 at 12:45 AM · quaternionlerpcube

Problem rotating a cube with quaternions using lerp

I'm trying to rotate a cube 90 degrees. My script was working until I started to simplify it :( I've starred myself blind as to why it's not working anymore. The cube just rotates a tiny bit and then starts jittering in its rotating movement.

     const float ROTATION_SPEED = 4.0f;
 
     Quaternion _sourceRotation;
     Quaternion _targetRotation;
 
     void Start () {
         _sourceRotation = this.transform.rotation;
         _targetRotation = this.transform.rotation;
     }
     
     void Update () {
 
         float angle = Quaternion.Angle(this.transform.rotation, _targetRotation);
         if (angle < 0.1f) {    
             _sourceRotation = _targetRotation;
             _targetRotation = this.transform.rotation * Quaternion.Euler(90, 0, 0); //a random rotation will be chosen here, but hardcoded right now for simplicity
         }
 
         this.transform.rotation = Quaternion.Lerp(_sourceRotation, _targetRotation, Time.deltaTime * ROTATION_SPEED);
     }

If I change _sourceRotation to a Transform instead of a Quaternion and store the cubes transform instead or its rotation in _sourceRotation, and in the Quaternion.Lerp use _source.rotation it does the rotation as it should, but I don't understand why... feeling frustrated

Comment
Add comment · Show 2
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 reinfeldx · Feb 19, 2015 at 06:16 PM 1
Share

As I just started working with Lerp myself, I don't have a full answer for you, but it might be worth noting that Unity's official video tutorial here is flawed.

Information on why it is flawed: http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

It looks to me like the third argument for Lerp in the last line of your code might be the problem. Again, not sure, but trying to point you in the right direction.

avatar image nesdroc · Feb 19, 2015 at 07:57 PM 0
Share

Thank you. It was confusing me a bit, since I also seem to recall what hexagonius writes about t [0;1].

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hexagonius · Feb 19, 2015 at 06:40 PM

That's because Lerp doesn't work the way you are using it. It interpolates from the first rotation to the second rotation while t == 0 is the first and t == 1 the second rotation value. Assume Time.deltaTime stays constant at 0.05 then t will always be 0.05*4 = 0.2. That's 20% the first and 80% the second rotation, that's where the jitter comes from, slightly different deltaTime every frame.

Create another float, use it as t, and add the Time.deltaTime * ROTATION_SPEED to it. Whenever you reach your target, reset it to 0.

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 nesdroc · Feb 19, 2015 at 07:59 PM 0
Share

Thanks a lot! However, do you know why it acts correctly when I use a stored transform.rotation ins$$anonymous$$d of a stored quaternion as I write in the latter part of the problem?

avatar image hexagonius · Feb 19, 2015 at 08:07 PM 1
Share

That's because saving the rotation creates a copy of the Quaternion and saving the transform a reference to it. With that you are reading the current rotation all the time and the lerp gets ever closer to its target. I would even say it's slowing down at the end.

avatar image nesdroc · Feb 19, 2015 at 08:15 PM 0
Share

Ah, yeah! That's right had been wondering about these things already, but only looked to the Quaternions and found out that they were passed by value! Thanks a lot :) I just started to look at some things again and hate when these kinda small things gets in the way.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to use LookAt() to make a gameobject look at another gameobject slowly? 0 Answers

Playmaker Script Issue with Quaternion Lerp speed value 0 Answers

how do i use Quaternion lerp on only the Y and Z axis 0 Answers

How to rotate towards the mouse on one axis while snapping 0 Answers

Error: NullReferenceException: Object reference not set to an instance of an object 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