Rotate an object 90 degrees in y axis "everytime" i click vertical key

Hello,
I have referred various forums. But i still have problems. Here is my code.

Quaternion rotationAmount = Quaternion.Euler(0, 90, 0);
Quaternion postRotation = transform.rotation * rotationAmount;
transform.rotation = postRotation;

I want the object to rotate 90 degrees “everytime” i press the vertical key button. Say initially the object is at 0 degrees, it should rotate to 90, then 180, then 360 then 0, whenever i press the key.

Thanks so much in advance for your help.

transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 90f, transform.rotation.eulerAngles.z); though, you can optimize it a bit since transform is quite expensive. You better store a Transform component in a variable like myTransform.

Hello @TheDiamondPlay, Thanks for your reply.
But the solution doesn’t work for me. when i use your code, the object rotates in an unpredictable way… whenever i press the button, first it rotates -180, then 90 then -90 then +90 and so on. The object has a rigidbody attached to it. Does it have any impact on the rotation??,Hello DiamondPlay, Thanks for your reply.
But the solution doesn’t work for me. when i use your code, the object rotates in an unpredictable way… whenever i press the button, first it rotates -180, then 90 then -90 then +90 and so on. The object has a rigidbody attached to it. Does it have any impact on the rotation??