# How to stop MoveRotation when rigidbody is facing a specific euler angle

I have a cup which has some objects inside. I want to rotate the cup from a script so it lets the objects fall. I must use Rigidbody.MoveRotate to avoid the objects pass through the cup’s mesh while it’s moving.
So default cup’s rotation (euler) is (0,0,0) and it must rotate on both x and y axis to (135,70,0). The problem is how to know when the cup has reached the correct rotation so I can stop MoveRotation to keep rotating forever.

Here is my code:

``````void Update () {
Vector3 rota = new Vector3(135, 70, 0);
Quaternion rotationDelta = Quaternion.Euler(rota * Time.deltaTime);
Rigidbody rigid = GetComponent<Rigidbody>();

rigid.MoveRotation(rigid.rotation * rotationDelta);

}
``````

I have tried to check:

`````` if (Quaternion.Angle(transform.rotation, Quaternion.Euler(rota)) > angleThreshold)
``````

But it won’t work because in real time Euler angles of the object are so messed up by the quaternion rotation (because I’m rotating 2 axis at once and the rotation on 1 axis is actually moving the other axis as it’s rotating and viceversa, that’s why unity uses quaternions in the first place)

Any help?

The solution toward your problem is relatively similar to the answer from this question.

However, due to your rigidbody-related constraints, it needs some little modification and the code below should work.

``````public float speed = 1;

float ratio = 0;
Quaternion rotationA = Quaternion.Euler(0, 0, 0);
Quaternion rotationB = Quaternion.Euler(135, 70, 0);

public void Update()
{
ratio += Time.deltaTime;
ratio = Mahtf.Clamp(ratio, 0f, 1f);
rigid.MoveRotation(Quaternion.Lerp(rotationA, rotationB, ratio));

if ( ratio == 1 )
print("it is stopped now")
}
``````

I wish this helps.

Just use vector3.angle

``````Transform AimTransform;  //this object will hold the target rotation

AimTransform.Lookat(someTarget);

if (Vector3.angle(transform.forward, AimTransform.forward) > .5f)
{
rb.moveRotate(someTarget)
}
``````