Unity Object Rotation

One of the things that has caused me a bit of confusion while working with Unity3D is object rotation. I was working on a small project today in which I was trying to build a four-walled building using C# code. I knew how to build a wall out of cubes using a nested “for” loop. But trying to rotate the objects 90 degrees so I could build four walls, it took me a while to figure out. Back in my days of working with Flash, you simply set the rotation property to the degrees you wanted it to rotate. But that was a 2-D environment. Unity has 3 dimensions, so rotation is more complicated.

To rotate 90 degrees, I found this example from Instantiate a Rotated Object.

Instantiate (object, position, Quaternion.Euler(0, 90, 0));

So it is similar to doing a rotation in Flash, only you have to tell it the axis to rotate on. In the above example, the object is rotated 90 degrees about the y axis, exactly what I was trying to do. I was able to build four walls using this tidbit.

Four Wall Example

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *