Question by
infilrtrator_55 · Oct 25, 2015 at 08:52 AM ·
c#rotationquaternion
Know when the object is rotated?
I'm using Quaternion.Slerp to rotate an object to face another object in the scene. I have some code to execute when the rotation is done. The problem is I cant figure out when the rotation is done, so I can call the required functions after that. I've tried comparing quaternions and the Y rotations of the objects in an if statement like this:
if (transform.rotation.y == obj.transform.rotation.y)
{
//code here
}
For now the required code gets executed in Upadate() while the object is rotating and it looks very unrealistic. Is there a way or function that lets you know when an object is rotated in a certain way??
Comment
Best Answer
Answer by toromano · Oct 25, 2015 at 02:52 PM
You can not compare two float values with "==". Using a threshold value might be useful in that case:
private bool isRotating = false;
void Update()
{
float angle = Quaternion.Angle(transform.rotation, target.rotation);
if (Mathf.Abs(angle) <.005f)
{
ısRotating = true;
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, Time.time * speed);
}
else
{
ısRotating = false;
}
}