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 /
This question was closed Nov 15, 2020 at 09:57 PM by mantczak for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mantczak · Nov 14, 2020 at 11:19 PM · rotationtransformlerptransform.rotationlocalrotation

transform.localRotation lerp precision

Hello,

I've got a method which is responsible for lerping rotation using steps, each step is 5% of rotation distance:

     IEnumerator Test()
     {
         int steps = 0;
         for (int i = 0; i < 20; i++)
         {
             float stepValue = currentPosition.localRotation.y - leftTileTarget.localRotation.y * .05f;
             //print("1. stepValue: " + stepValue);
             //print("2. local rotation value: " + transform.localRotation.y);
             float newRotationValue = Mathf.Abs(transform.localRotation.y) + stepValue;
             //print("3. new rotation value: " + newRotationValue);
             float step = newRotationValue / Mathf.Abs(leftTileTarget.localRotation.y);
             print("4. step " + step);
             // TODO how to avoid changing decimal points value
             transform.localRotation = Quaternion.Lerp(currentPosition.localRotation, leftTileTarget.localRotation, step);
             //print("5. local rotation y after lerp: " + transform.localRotation.y);
             //print("6. lerp value: " + Quaternion.Lerp(currentPosition.localRotation, leftTileTarget.localRotation, step));
             steps++;
             if (transform.localRotation == leftTileTarget.localRotation)
             {
                 print(steps);
                 break;
             }
             yield return null;
         }
     }

I've got also similar function which lerp to target position also using 5% distance step:

     IEnumerator Test2()
     {
         int steps = 0;
         for (int i = 0; i < 20; i++)
         {
             float positionDistance = Mathf.Abs(currentPosition.localPosition.z - leftTileTarget.localPosition.z);
             float positionStepPercentageValue = positionDistance * .05f;  // move step float value necessary for calculating step percentage value
             float newPositionValue = Mathf.Abs(transform.localPosition.z) + positionStepPercentageValue;  //  new z value after incementation by moveValue
             float positionStep = newPositionValue / positionDistance;  // step calculated using new position and percentage value of step distance provided by CheckRectPosition()
             transform.localPosition = new Vector3(0, 0, Mathf.Lerp(currentPosition.localPosition.z, leftTileTarget.localPosition.z, positionStep));
             steps++;
             if (transform.localPosition == leftTileTarget.localPosition)
             {
                 print(steps);
                 break;
             }
             yield return null;
         }
         print(steps);
     }
 

My problem is rotation lerp - When I'm lerping to target rotation after first lerp I'm getting issue with decimal number: alt text Lerp step value is: 0,03043807 provided as a interpolation step. But its lerping to 0,03074131 (minus is intended and it's fine) But the 0,0003 difference is generating huge miscalculation which results in finishing in 16 steps instead of 20...

And ideas what I'm doing wrong and how can I solve this?

sample.png (37.8 kB)
Comment
Add comment · Show 3
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 andrew-lukasik · Nov 14, 2020 at 10:31 PM 0
Share

When precision is something you're after - do not use Quaternion.Lerp (linear/vector interpolation) but Quaternion.Slerp (spherical/rotation interpolation).

avatar image andrew-lukasik · Nov 14, 2020 at 11:18 PM 0
Share

Your first line alone contains a math error that turns everything wrong:

 float stepValue = currentPosition.localRotation.y - leftTileTarget.localRotation.y * .05f;

as 20-10*0.5 is 15 not 5.

avatar image mantczak andrew-lukasik · Nov 14, 2020 at 11:25 PM 0
Share

I found that math error and I used $$anonymous$$ath.Abs() - same as in Test2(). Thanks for suggesting Slerp I will check your solution when I get back to home. :)

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by andrew-lukasik · Nov 14, 2020 at 11:55 PM

This is a proper way to interpolate rotations:


interpolation

 using UnityEngine;
 public class InterpolateRotationInNSteps : MonoBehaviour
 {
     [SerializeField] Transform _target;
     [SerializeField][Min(1)] int _numSteps = 20;
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         if( _target==null ) return;
 
         Vector3 pos = transform.position;
         Quaternion rot_initial = transform.rotation;
         Quaternion rot_target = _target.rotation;
 
         Gizmos.color = Color.black; Gizmos.DrawLine( pos , pos + rot_initial*Vector3.right );
         Gizmos.color = Color.white; Gizmos.DrawLine( pos , pos + rot_target*Vector3.right );
 
         for( int step=1 ; step<=_numSteps ; step++ )
         {
             float progress = (float)step / (float)_numSteps;
             Quaternion rot_now = Quaternion.Slerp( rot_initial , rot_target , progress );
             
             Gizmos.color = Color.Lerp( Color.blue , Color.green , progress );
             Gizmos.DrawLine( pos , pos + rot_now*Vector3.right );
         }
     }
     #endif
 }
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 mantczak · Nov 15, 2020 at 06:27 PM 0
Share

Thanks for your solution it worked :)

avatar image andrew-lukasik mantczak · Nov 15, 2020 at 07:02 PM 0
Share

Proszę bardzo.

Follow this Question

Answers Answers and Comments

212 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 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

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

How to rotate 90 degrees from 270 degrees to 0 with lerp? c# 1 Answer

Objects not rotating right half the time based on another object 0 Answers

Quaternion lerp is not rotating my object to the desired rotation? 1 Answer

Rigidbody rotation vs Transform rotation 4 Answers


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