Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Quaternion.Lerp rotate too soon

i use this code to rotate my avatar to targetPostion, but the problem is it alway rotate too soon & faster then the duration.

any ideas?

IEnumerator MoveToPosition(GameObject avatar, Vector3 targetPosition, float speed)
{
   // Rotate towards the target
   Vector3 direction = targetPosition - avatar.transform.position;
   Quaternion toRotation = Quaternion.LookRotation(direction);

   float STARmove = 0;
   float duration = 1;

   while (STARmove < duration)
   {

       STARmove += Time.deltaTime;


       // Calculate rotation based on interpolation ratio
       float t = Mathf.Clamp01(STARmove / duration);
       avatar.transform.rotation = Quaternion.Lerp(avatar.transform.rotation, toRotation, t);

       // Wait for a fixed time before next update
       yield return new WaitForSeconds(Time.deltaTime);
   }} 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Interpolate between the start and end rotations, not between the current and end rotations.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading