- Home /
Quaternion.Lerp is not behaving as expected
I'm using the below script to try and turn an object 180° around its local y-axis without stopping until it's either facing right, or left on the screen, and all within the time frame of the variable rotationTime. Because I'm driving the Lerp function with an alternating increment/decrement time variable, the user should be able to interrupt the current rotation and begin turning back to face the previous direction.
The problem is that no matter what I try, the rotation appears to occur almost instantly, and appears to ignore any change in the variable rotationTime instead of being driven by the currentRotationTime /rotationTime parameter in the Lerp function. I've been over this more times than I can count, and I'm not seeing where I've gone wrong.
Just about the only thing I can think of, is that it's due to my changing the lookRotation on each iteration, instead of simply giving it two static rotation values and telling it to Lerp between them. That seems like that would be a pretty severe limitation of Quaternions though and that it shouldn't matter, because the Lerp is controlled by time, and should simply recalculate the new rotation based on all it's current parameters.
Any help is greatly appreciated. Thanks! using UnityEngine;
public class PlayerInput : MonoBehaviour {
[Range(0f, 2f)]
public float rotationTime;
private Quaternion pendingRotation;
private float currentRotationTime;
private bool isRotating;
private Vector3 directionVector;
private Vector3 previousDirectionVector;
void Start () {
directionVector = Vector3.right;
previousDirectionVector = Vector3.right;
pendingRotation = Quaternion.identity;
currentRotationTime = 0.0f;
}
void Update () {
// Get the right sticks input
Vector3 rightStick = new Vector3(Input.GetAxisRaw("RightStickX"), Input.GetAxisRaw("RightStickY"), 0.0f);
// Is there any new input?
if (rightStick != Vector3.zero) {
// Get the input and normalize it to a vector
directionVector = rightStick.normalized;
// User says we should be facing left?
if (directionVector.x < 0f) {
// This is for another part of the script that isn't included here.
// Magnitde shouldn't matter, just direction, so clamp the value to -1.0
//so that we're only concerned about the y-value
directionVector.x = -1.0f;
// Initiate a new rotation, or continue processing a previous rotation?
if (previousDirectionVector.x > 0.0f) {
isRotating = true;
}
}
// User says we should be facing right
else if (directionVector.x > 0f) {
// This is for another part of the script that isn't included here.
// Magnitde shouldn't matter, just direction, so clamp the value to -1.0
// so that we're only concerned about the y-value
directionVector.x = 1.0f;
// Initiate a new rotation, or contine processing a previous rotation?
if (previousDirectionVector.x < 0.0f) {
isRotating = true;
}
}
// Get a copy of this loops input vector for the next loop
previousDirectionVector = directionVector;
}
// Begin processing a new rotation, or continue processing an existing rotation
if (isRotating) {
Quaternion tempRotation1 = Quaternion.identity;
Quaternion tempRotation2 = Quaternion.identity;
// If the player should be facing right, decrement the numerator to zero
if (directionVector.x == 1.0f) {
currentRotationTime -= Time.deltaTime;
tempRotation1 = Quaternion.LookRotation(directionVector, Vector3.up);
tempRotation2 = transform.rotation;
}
else { // The player should be facing left, increment the numerator to rotationTime
currentRotationTime += Time.deltaTime;
tempRotation1 = transform.rotation;
tempRotation2 = Quaternion.LookRotation(directionVector, Vector3.up);
}
// Clamp the time values so that we end facing exactly right/left
currentRotationTime = Mathf.Clamp(currentRotationTime, 0.0f, rotationTime);
// With currentRotationTime of 0.0f, we should get tempRotation1
// With currentRotationTime == rotationTime, we should get tempRotation2
pendingRotation = Quaternion.Lerp(tempRotation1, tempRotation2, currentRotationTime / rotationTime);
// There are no more rotations to process, set the flag
if ((currentRotationTime >= rotationTime) || (currentRotationTime <= 0.0f)){
isRotating = false;
}
}
// Apply the rotation to the object
transform.localRotation = pendingRotation;
}
Answer by mroel · Feb 09, 2019 at 12:33 PM
Hi Double_D, did yo figure out a solution to this? I'm having the same issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084341im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player 1 Answer
Lerp Rotation C# 3 Answers
Quaternion.Lerp shaky while in coroutine? 1 Answer
better way to rotate instead Coroutine 1 Answer
Rotation jumping to a different rotation after rotating a certain amount 0 Answers