Rotate objects to face variable direction

I have a set of walls (in the shape of a square) that I’m spawning around a point that the player is looking at. I know the Vector3 of each of the wall segments as they are added (iteratively) around that point. The problem that I’m having is making them point in the right direction. As stated before, it’s in the shape of a square, so the section of walls in front of the player needs to look away from the center in the same direction the player is looking. Inversely, the section opposite it needs to look in the opposite direction, and the same thing for the right and left sections. How do I take the given Vector3’s and transform them to point in the right direction? With hard coded values, I can get it to work for the cardinal directions, but then it slowly screws up as you approach the next 90 degrees.

From a 2D standpoint, so either X,Y or X,Z most likely, you feed those two numbers into:

https://docs.unity3d.com/ScriptReference/Mathf.Atan2.html

That gives you an answer in radians, which you can convert to degrees by multiplying it by:

https://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html

It may be necessary to swap / negate various of the inputs to get it right based on how your prefab/scene is set up.

1 Like