Why it’s taking so much time to the object to rotate facing another object?

using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; using FIMSpace.FLook;  public class Rolling : MonoBehaviour {     public GameObject mainCam;     public Transform navi;     public MoveToTarget moveToTarget;     public CinemachineFreeLook freeLookCamGameplay;     public CinemachineFreeLook freeLookCamPickUp;     public Transform player;     public Transform target;     public Transform crate;     public Animation anim;     public UnlockCrate unlockCrate;     public float timeToStopRolling;     public float force;     public float playerRotatingSpeed;     public float waitBeforeSlowdown;     public float startDrag;     public float endDrag = 50;     public float rotationSpeed;     public float speed;     public static bool hasStopped = false;     public bool go = false;      private bool crateOpenOnce = false;     private bool alreadyRolling;     private Rigidbody rb;     private Quaternion defaultRotation;     private Animator animator;     private bool startSlow = true;     private bool startSpeedUp = false;     public float lerpTimeMultiplicator = 0.25f;      private float timeElapsed = 0;     public float lerpDuration;     private float startValue = 1;     private float endValue = 0;     private float valueToLerp = 0;      private bool playonce = false;      private void Start()     {         rb = GetComponent<Rigidbody>();          defaultRotation = rb.rotation;          animator = player.GetComponent<Animator>();     }      private void Update()     {         if (go)         {             //var distance = Vector3.Distance(crate.position, player.position);             if (crateOpenOnce == false && unlockCrate.HasOpened())             {                 navi.GetComponent<InteractableItem>().enabledInteraction = true;                 rb.isKinematic = false;                 crateOpenOnce = true;             }              if (startSpeedUp)             {                 if (playonce == false)                 {                     animator.Play("Walk Backward");                     playonce = true;                 }                  StartCoroutine(ChangeSpeed(0, 1, 3f));                  var lookPos = target.position - player.position;                 lookPos.y = 0;                 var rotation = Quaternion.LookRotation(-lookPos);                 player.rotation = Quaternion.Slerp(player.rotation, rotation,                     Time.deltaTime * rotationSpeed);                  float playertargetdistane = Vector3.Distance(player.position, target.position);                  if (playertargetdistane < 1f && startSlow && animator.GetFloat("Walk Backward") == 1f)                 {                     StartCoroutine(ChangeSpeed(1, 0, 3f));                      startSlow = false;                     startSpeedUp = false;                 }                  if(playertargetdistane < 1.06f)                 {                     if(animator.GetFloat("Walk Backward") == 0)                     {                         animator.SetTrigger("Is Walking Back");                     }                 }             }         }     }      private void OnCollisionEnter(Collision collision)     {         if (go)         {             //NOTE: In general you should go for Tags instead of the name             if (collision.gameObject.name == "Crate_0_0")             {                 if (crateOpenOnce)                 {                     rb.drag = 0f;                      startSpeedUp = true;                      // Directly start a routine here (if none is already running)                     if (!alreadyRolling) StartCoroutine(RollingRoutine());                 }             }         }     }      private void OnCollisionExit(Collision collision)     {         if (go)         {             if (collision.gameObject.name == "Crate_0_0")             {                 var brain = mainCam.GetComponent<CinemachineBrain>();                 brain.m_DefaultBlend.m_Time = 1f;                 freeLookCamGameplay.enabled = false;                 freeLookCamPickUp.enabled = true;             }         }     }      private Vector3 GetRandomDirection()     {         var rnd = Random.insideUnitSphere;         rnd.y = 0;         return rnd.normalized;     }      private IEnumerator RollingRoutine()     {         // Just in case prevent concurrent routines         if (alreadyRolling) yield break;          // Block new routines from starting         alreadyRolling = true;          // Get the random direction for this routine         var rollDirection = GetRandomDirection();          // Roll for the given time within the FixedUpdate call         for (var timePassed = 0f; timePassed < timeToStopRolling; timePassed += Time.deltaTime)         {             // Wait until you are in FixedUpdate             // the code after this is now executed within FixedUpdate             yield return new WaitForFixedUpdate();              rb.AddForce(Vector3.right /*rollDirection*/ * force * Time.deltaTime);         }          // Wait before slowing down         yield return new WaitForSeconds(waitBeforeSlowdown);          // Do slow down and rotate to default until both conditions are fulfilled         var dragLerpFactor = 0f;         // Store the original drag to reset it later         var defaultDrag = rb.drag;         while (!Mathf.Approximately(rb.velocity.sqrMagnitude, 0) || rb.rotation != defaultRotation)         {             // Again wait until you are in FixedUpdate             yield return new WaitForFixedUpdate();              dragLerpFactor += Time.deltaTime * lerpTimeMultiplicator;             rb.drag = Mathf.Lerp(startDrag, endDrag, dragLerpFactor);              rb.MoveRotation(Quaternion.RotateTowards(rb.rotation, defaultRotation, rotationSpeed * Time.deltaTime));         }          // Just to be sure to end with clean value assign once         rb.rotation = defaultRotation;         rb.drag = defaultDrag;         rb.velocity = Vector3.zero;          hasStopped = true;          Destroy(transform.GetComponent<Rigidbody>());         Destroy(transform.GetComponent<SphereCollider>());     }      IEnumerator ChangeSpeed(float v_start, float v_end, float duration)     {         float elapsed = 0.0f;         while (elapsed < duration)         {             speed = Mathf.Lerp(v_start, v_end, elapsed / duration);             animator.SetFloat("Walk Backward", speed);             elapsed += Time.deltaTime;             yield return null;         }         speed = v_end;     } } 

The problem is in this part : This part in the while is taking a lot of time almost more than a minute to end.

while (!Mathf.Approximately(rb.velocity.sqrMagnitude, 0) || rb.rotation != defaultRotation)         {             // Again wait until you are in FixedUpdate             yield return new WaitForFixedUpdate();              dragLerpFactor += Time.deltaTime * lerpTimeMultiplicator;             rb.drag = Mathf.Lerp(startDrag, endDrag, dragLerpFactor);              rb.MoveRotation(Quaternion.RotateTowards(rb.rotation, defaultRotation, rotationSpeed * Time.deltaTime));         } 

The goal is to make the object to rotate facing another object and I see it rotating but it’s taking a lot of time until it’s ending to rotate when it looks like it ended rotating it’s still not getting out of the while loop.

It seems like it’s not ending the rotation even if the rotation speed is 3 or even if the rotation speed is much faster still it’s taking a lot of time to end the rotation not to rotate. It’s rotating in fine speed but near the end or at end it’s taking a lot of time.

This screenshot is the settings of the script in the editor all the values of the float/bool variables :

Rolling script settings in the editor