- Home /
Coroutine with Quaternion.Angle
Im trying to rotate cube to 0 or 90. Problem is that my coroutines start to piling up and I have no idea why. This script is on cube.
using UnityEngine;
using System.Collections;
public class RotationTest : MonoBehaviour {
public bool isRotating = false;
public float turningSpeed;
public int rotationAngle = 90;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!isRotating) {
rotationAngle = (rotationAngle == 90) ? 0 : 90;
StartCoroutine(RotateTo(rotationAngle));
}
}
private IEnumerator RotateTo(int to) {
isRotating = true;
Quaternion fromPos = transform.rotation;
Quaternion toPos = Quaternion.Euler(0, to, 0);
while (Quaternion.Angle (transform.rotation, toPos) > 0.1f) {
transform.rotation = Quaternion.Slerp (fromPos, toPos, Time.time * turningSpeed);
yield return null;
}
isRotating = false;
}
}
Can someone tell me what im doing wrong there. Thanks.
Answer by whydoidoit · Mar 11, 2014 at 06:35 PM
So you aren't using Slerp correctly which means that it's exiting too quickly. If you want to Slerp over a fixed time add Time.deltaTime onto a variable you start at 0 in your coroutine. If you are looking for a damped slerp then use Time.deltaTime instead of Time.time (which after the first second of the game will be guaranteed to complete the Slerp immediately).
Got it.
Quaternion.Slerp (fromPos, toPos, (Time.time - startTime) * turningSpeed);
Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613140009im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
making a camera that moves depending on mousepos.y 0 Answers
Measuring the Angle Between Two Transforms for t Param of Quaternion.Slerp 1 Answer
Slerp Finishes Rotation When Progress is 0.5f 1 Answer
Coroutine While loop help 2 Answers
Quaternion lerp is not rotating my object to the desired rotation? 1 Answer