Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with rotations and quaternions

Discussion in 'Scripting' started by CreationFMR, Mar 31, 2021.

  1. CreationFMR

    CreationFMR

    Joined:
    Nov 7, 2020
    Posts:
    22
    I have this script which run until the transform reaches a specific vector3 (positionTarget) which looks like this:

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.  
    4.         if (Vector3.Distance(transform.position, positionTarget) > 0.01f)
    5.         {
    6.             var newPosition = (positionTarget - transform.position).normalized;
    7.             transform.position = transform.position + newPosition * Time.deltaTime * speed;
    8.         }
    9.     }
    Easy enough, now I'm trying to figure out how to write the same behavior for rotation. I know this script doesn't make any sense and doesn't work but here's one of the many variations I have tried. (Just to represent the idea)

    Code (CSharp):
    1.             if (Vector3.Distance(rotationTarget.eulerAngles, transform.rotation.eulerAngles) >/ 0.01f)
    2.             {
    3.  
    4.                 var newRotation = (rotationTarget.eulerAngles - transform.rotation.eulerAngles).normalized;
    5.                 transform.rotation = transform.rotation + newRotation * Time.deltaTime * speed;
    6.  
    7.             }
    Here the rotationTarget is: [SerializeField] Quaternion rotationTarget;

    I had this script for rotation before, but the thing is, I don't want to use Lerp. I want a constant speed.

    Code (CSharp):
    1. if (rotationFraction < .99f)
    2.             {
    3.                 rotationFraction += lerpSpeed * Time.deltaTime;
    4.                 rotationFraction = Mathf.Clamp01(rotationFraction);
    5.                 transform.rotation = Quaternion.Lerp(transform.rotation, rotationTarget, rotationFraction / 60f);
    6.             }
    Can anyone point me in the right direction? I really would like to understand how to script the rotation behavior like the position. Here the pseudo code:

    If (the difference between the current rotation and the target rotation is greater than "super small amount")
    rotate to the target rotation at constant speed;
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Lerp does do a constant speed when you do it properly (that's why it's called a linear interpolation).

    But you can also use Quaternion.RotateTowards to get a linear interpolation pretty simply:

    Code (CSharp):
    1. if (Quaternion.Angle(rotationTarget, transform.rotation) > 0.01f)
    2. {
    3.    transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationTarget, TIme.deltaTime * speed);
    4. }
     
    Bunny83 likes this.
  3. CreationFMR

    CreationFMR

    Joined:
    Nov 7, 2020
    Posts:
    22
    Oh thanks PraetorBlue! I had tried this condition, but I didn't know about Quaternion.RotateTowards. This is great! Works like a charm!