using a custom rotation in "Instantiate"

I’m calling a GameObject to load at a specific point. I originally called it like this:

building = Instantiate(farmingPlot, farmPosition, transform.rotation );

However I need it to rotate 45 degrees in the x axis. Therefore I tried calling it like this:

building = Instantiate(farmingPlot, farmPosition, (transform.rotation.x-45,transform.rotation.y,transform.rotation.z) );

This is not working though. Does anyone know how I can do this?

You don’t want to be using the x,y,z and w values of a rotation directly unless you really understand Quaternions. In fact, I’m surprised this compiled. Use Quaternion.Euler() instead:

building = Instantiate(farmingPlot, farmPosition,  Quaternion.Euler(Vector3(45, 0, 0)));

This will give an absolute rotation of 45 degrees. If you want it relative like you have it, you can add transform.eulerAngles.y to the value.

I was looking for my problem too, and what I wanted to just rotate 90 degrees when I look just straight to sprite. It was Z instead of Y axis.

Instantiate (fish, spawnPosition, Quaternion.Euler(new Vector3(0, 0, -90)));

alt text

I wanted to update this since I came across it if you put the Vector3 call in the Quaternion.Euler call your code will not compile. Just drop the Vector3() portion off and your golden. See documentation: Unity - Scripting API: Quaternion.Euler

var spawnTemp = Instantiate(prefab, gameObject.transform.position, Quaternion.Euler(270, 0, 0));