Hello, I’m trying to snap an object automatically to an 90° or 0° angle between the x and z axis.
So if the player moves the object to an angle of, e. g. 70° (between BAC (where B is the object I want to move)), this means that the object should snap to the x axis - or if it’s less than 45° to the z axis.
Basically, the object B should have an identical z-axis value with the object A, if the angle is more than 45° and vice versa. Please also take a look at the attached image for better understanding.
Simple make point A rotate where Object B is and read its rotation. Then when it reaches snap point just by doing B.transform.position = C.transform.position move B to snap point.
So I’m assuming this is a 3D game. and your worrying about the rotation around the Y-Axis (spinning around in place, like a guy looking all aorund).
One other thing to note, your x and Z axis are flipped from normal conventions. Normally Z would be forward (into the screen) and x is the left/right
Another to to realize is 0 rotation around the y-Axis means we are pointing directly forward towards spot C +90 y rotation would make us point towards D
The following code will snap an object’s y rotation to 0 or 90:
Now the tricky part is knowing when to call this code. You can’t just call it in an Update since as soon as I start rotating my guy it will instantly snap me back to 0. I won’t ever make it to 45 to get to the 90 snap point. You will need someway to know when the user stops rotating. Then when that happens call this code. For example if your user rotates by holding down the right mouse button and moving the mouse… just call this code when you get a rightmousebutton Up.
Edit the same concepts apply to a 2D game. Though in 2D your considered to be straight forward along the z-Axis at a plane. So all rotations should be down around the Z-Axis.