0
\$\begingroup\$

It seems that when the player starts rotating it looks like a small "jump" of the rotation and then the rotation gets smooth.

At the top of the script : because I want to rotate on the Y only :

Quaternion startRotation;
Quaternion endRotation;
float rotationProgress = -1;

In Start()

void Start()
{
    
    StartRotating(-40f);
}
    
void StartRotating(float yPosition)
{
    
    // Here we cache the starting and target rotations
    startRotation = transform.rotation;
    endRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, yPosition, transform.rotation.eulerAngles.z);
    
    // This starts the rotation, but you can use a boolean flag if it's clearer for you
    rotationProgress = 0;
}

Then in FixedUpdate()


private void FixedUpdate()
{
    if (startRotatingBack)
    {
        if (rotationProgress < 1 && rotationProgress >= 0)
        {
            rotationProgress += Time.deltaTime * rotationSpeed;
    
            // Here we assign the interpolated rotation to transform.rotation
            // It will range from startRotation (rotationProgress == 0) to endRotation (rotationProgress >= 1)
            transform.rotation = Quaternion.Lerp(startRotation, endRotation, rotationProgress);
            //transform.GetComponent<Rigidbody>().MoveRotation(Quaternion.Lerp(startRotation, endRotation, rotationProgress));
        }
    
        if (exitSpaceShipSurroundingArea)
        {
    
            playerAnimator.SetFloat("Forward", Mathf.Lerp(min, max, t));
            t += 1f * Time.deltaTime;
        }
        else
        {
           var distance = Vector3.Distance(transform.position,spaceShip.position);
    
           distanceText.text = distance.ToString();
    
           if (distance < 90f)
           {
               if (slowDownBack == false)
               {
                   playerAnimator.SetFloat("Forward", Mathf.Lerp(min, max, t));
                   t -= 1f * Time.deltaTime;
               }
    
               if (playerAnimator.GetFloat("Forward") == 0)
               {
                  ThirdPersonUserControl.stop = false;
                  slowDownBack = true;
                  startRotatingBack = false;
               }
           }
        }
    }
}

I tried also using Rigidbody MoveRotation but with the same result the first frame or some frames the rotation is like "jumping" then getting smooth. not sure why it's not smooth slowly rotating from the beginning.

Tried also this but still, the beginning is not smooth.

if (rotationProgress < 1 && rotationProgress >= 0)
{
    transform.rotation = Quaternion.Lerp(startRotation, Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, -40f, transform.rotation.eulerAngles.z)), rotationProgress);
    rotationProgress += Time.fixedDeltaTime * rotationSpeed;       
}

In this case, it should rotate smoothly towards degrees -40 the main goal is to rotate the transform towards a target but it's not rotating smoothly at the beginning yet.

I solved it :

The problem was that I referenced for the transform rotation at the Start() but the transform rotation was not the same when start rotating.

The solution is to start rotating from the current rotation :

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, -40f, transform.rotation.eulerAngles.z)), rotationProgress);

This part :

Quaternion.Lerp(transform.rotation

And NOT :

Quaternion.Lerp(startRotation
\$\endgroup\$
2
  • \$\begingroup\$ sub question : If my character have a Rigidbody do I must to rotate and move the character in FixedUpdate with MovePosition and MoveRotation or I can do it in Update() and use regular rotation,position ? \$\endgroup\$ – Sharper Then Ever Jun 4 at 22:18
  • \$\begingroup\$ The main difference is that you can think of moving/rotating through Rigidbody as sliding while transform modifications are more like "teleporting" (even if at tiny distances that resemble sliding). There are a lot of nuances but generally if there is a collider and you use transform you force Unity to do a bit more work. \$\endgroup\$ – Nikaas Jun 5 at 11:15
1
\$\begingroup\$

The problem was that I referenced the transform rotation at the Start() but the transform rotation was not the same when start rotating.

The solution is to start rotating from the current rotation :

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, -40f, transform.rotation.eulerAngles.z)), rotationProgress);

This part :

Quaternion.Lerp(transform.rotation

And NOT :

Quaternion.Lerp(startRotation
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.