I originally was using:
transform.Rotate
But it rotated the object…forever. I want to rotate my object 90 degrees left and then STOP the rotation. But transform.Rotate just continued it because it was in an Update function. How can I stop the rotation at 90 degrees.
Do I need to use a Quaternion? I’ve tried learning about Quaternions on Unity Scripting API but can’t seem to get my head around them - they seem more complicated than what I want to do.
Anyway, thank you if you know the answer to this!
How about using a coroutine, this is a rotate around method that will stop after the given number of degrees has been achieved:
bool isRotating = false;
public Vector3 this_Axis;
public float deg;
public float time;
void Update () {
if(Input.GetKey(KeyCode.Q) && !isRotating){
StartCoroutine(RotateAround(this_Axis, deg, time));
}
}
IEnumerator RotateAround(Vector3 axis, float degrees, float duration){
if(axis == Vector3.zero || degrees == 0){
return true;
}
isRotating = true;
float d = 0;
Quaternion q;
Quaternion startRot = transform.rotation;
float progress = 0;
while(progress <= 1){
d = Mathf.Lerp(0, degrees, progress);
q = Quaternion.AngleAxis(d, axis);
transform.rotation = startRot*q;
progress += Time.deltaTime/duration;
yield return null;
}
transform.rotation = startRot*Quaternion.AngleAxis(degrees, axis);
isRotating = false;
}
Hope that helps!
Scribe