# How to rotate on one axis by 90 degree increments

I am making an object that this script is attached to, face the player upon its creation. I only want it to possibly only face one of the four cardinal directions, aka either 0, 90, 180, or 270. I got the code working so it faces the player, but how do I “snap” it to the nearest “snap angle” I want? Here’s the code:

``````Vector3 faceVector = new Vector3(player.position.x - transform.position.x, 0.0f , player.position.z - transform.position.z);

transform.rotation = Quaternion.LookRotation(faceVector);
``````

Off the top of my head, I would divide the angle by 90, round to integer and multiply by 90 again.

You could use Mathf.RoundToInt () for Vesuvian’s sugestion,
or
you could make a 4 stage if else:
angle between (-45, 45] then the transform rotation is zero;
angle between (45, 135] then the transform rotation is 90;
angle between (135,225] then the transform rotation is 180;
angle between (225,315] then the transform rotation is 270.
Only mind the brakets (reminder from highschool): “(45” means 45,0000000…00001.
It gets more complicated if rotation gets above 360 degrees. You’ll have to get back in the 0, 360 range.