How to rotate only 90 degree with Rigidbody.MoveRotation

The question may be very simple but i am new and cant find any answer at documention.

How can i rotate a rigidbody at only 90 degree at z axis with Rigidbody.MoveRotation when any button is pressed.

Because the code below cause infinite rotation.

``````private Rigidbody cubeRB;
private Vector3 eulerAngleVelocity;
public float speed;
Quaternion deltaRotation;

// Use this for initialization
void Start () {

cubeRB = GetComponent<Rigidbody> ();

}

// Update is called once per frame
void Update () {

if (Input.GetKeyUp(KeyCode.UpArrow))
{
eulerAngleVelocity.Set(90,0f,0f);

}
if (Input.GetKeyUp(KeyCode.DownArrow))
eulerAngleVelocity.Set(-90,0f,0f);
if (Input.GetKeyUp(KeyCode.RightArrow))
eulerAngleVelocity.Set(0f,-90,0f);
if (Input.GetKeyUp(KeyCode.LeftArrow))
eulerAngleVelocity.Set(0f,90,0f);
}

void FixedUpdate()
{
deltaRotation = Quaternion.Euler(eulerAngleVelocity.normalized *speed* Time.deltaTime);

cubeRB.MoveRotation(cubeRB.rotation * deltaRotation);

}
``````

Did you read the document of `MoveRotation()` before using it. It rotates the rigid body to a given rotation. It’s not delta rotation, it’s absolute. Also it rotates the rigid body, not the transform. Use `transform.Rotate()` instead. I don’t know why you are doing the rotation and calculations in different update functions, it’s just waste of calculations. From the looks of it you wanna rotate about and axis, to it would be better to use `transform.RotateAround()`, here.

Also why are you using the `.Set` notation, it’s really odd, and counter intuitive. Just use `eulerVel.y = 90` etc.