- Home /
Quaternion.Slerp is not working as Quaternion.Euler has weird values
I just want a simple Rotation in the Y Axis with random values and have following code:
private void Rotate()
{
rotation = Random.Range(-360.0f, 360.0f);
print(rotation);
Quaternion qRotation = Quaternion.Euler(0, rotation, 0);
print(qRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, qRotation, Time.deltaTime * rotationSpeed);
}
But on Runtime the rotation is not working and the values for the Quaternion is also weird...
Even when i put fix flaots into the Quaternion.Euler, the rotation is not happening as the values for the qRotation will alway between 0 and 1...
Answer by WarmedxMints · Sep 21, 2019 at 04:39 PM
The qRotation value is correct. Quaternions do not show the rotation angle. Your problem is with how you are using the Slerp. That would need to be called each frame until it is complete. It interpolates from rotation A to rotation B by parameter T. Either set your rotation and then call the slerp every frame or use a coroutine like so;
public void RotateObj(float duration)
{
StartCoroutine(RotateActual(duration));
}
private IEnumerator RotateActual(float duration)
{
var startRot = transform.rotation;
var randomRot = Random.Range(-360f, 360f);
var endRot = Quaternion.Euler(0, randomRot, 0);
var t = 0f;
while (t <= 1f)
{
transform.rotation = Quaternion.Slerp(startRot, endRot, t);
t += Time.deltaTime / duration;
yield return null;
}
transform.rotation = endRot;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612212443im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
quaternion and interpolation: no reaction? 0 Answers
Transform rotation and position on key input: rotation only working the first time 2 Answers
Follow rotation on Y axis ONLY clockwise/counterclockwise. 1 Answer
Quaternion Slerp 180 Away from target 1 Answer
few question about quaternion slerp 3 Answers