- Home /
How to add rotation to current rotation over time?
I want to add a rotation to the current rotation of an object every time when I press the mouse-button. However there are some issues regarding the rotation behaviour e.g. when the local rotation amount is set greater 90, it lerps back after one iteration.
This is what my script looks like:
public bool lerping;
public float lerpSpeed;
public Vector3 localRotAmount;
private Vector3 startRot;
private Vector3 endRot;
private float ratio;
void InitRot(){
startRot = transform.eulerAngles;
endRot = startRot + localRotAmount;
}
void OnMouseDown() {
if(lerping)
return;
lerping = true;
ratio = 0f;
InitRot();
}
void Update(){
if(lerping){
ratio += lerpSpeed * Time.deltaTime;
transform.eulerAngles = Vector3.Lerp(startRot, endRot, ratio);
if(ratio >= 1f)
lerping = false;
}
}
I think that using eulerAngles for rotation-setting might be not powerful enough against using quaternions, however I am not sure how to handle with them in order to resolve this problem.
Answer by Noxury · Feb 18, 2017 at 04:56 PM
Nevermind, found the solution by changing startRot/endRot to Quaternions and using
endRot = startRot * Quaternion.Euler(localRotAmount);
Answer by ThisGuyThatsHere · Feb 18, 2017 at 03:04 PM
If you want to use Quernions simply change all the vectors through Quaternion.Euler(Vector3) and use Quaternion.Lerp, tho I'm not sure if this is the solution you are looking for.
Your answer
Follow this Question
Related Questions
Rotation jumping to a different rotation after rotating a certain amount 0 Answers
Make Quaternion Slerp happen over a 1 second period 3 Answers
How do you smoothly transition/Lerp into a new rotation? 3 Answers
Clarifying the ambiguities of rotation in Unity 1 Answer
Per-Axis (Euler) Rotation Control While Avoiding Gimbal Lock 1 Answer