# Set rotation for instantiated objects

`````` var thingy : GameObject;
var thingyPosition : Vector3;
var thingyRotation = ???
thingyClone = Instantiate (thingy, thingyPosition, thingyRotation);
``````

What should the thingyRotation var be?

Quaternion

Could you be more specific? If I set it as Quaternion, i have no clue how to set it correctly.

Why dont you go read the help filesâ€¦

I have.

Quarternion is a class. It is a mathematical property. It is confusing to one that does not know how it works. This includes me. An alternative is to have an object, with an example rotation. So, thingyRotation, could be a vector3, instead of a Quaternion. And then you could have thingyRotationExampleObject. And use thingyRotationExampleObject.rotation as your rotation.

What about the documentation didnt you understand?

It gives you several examples.

@Renman3000: not sure i understand, do you mean have an object in the scene some where with the rotation I want and set that rotation to the instantiated object?

@JamesLeeNZ: copy and paste from the doc on quaternions â€śDonâ€™t modify this directly unless you know quaternions inside outâ€ť

Yes.

You dont need to understand how they work. That comment is to stop people trying to mess with the individual numbers assuming they will get the rotation they expect.

The docs give plenty of good examples of how to get a rotation without understanding quatâ€™s.

Just use Quaternion.Identity if you dont know any better

Renman3000â€™s way works fine, thanks.

1 Like

Freeddooom!

Sorry, my parents are Scottish.

I was looking for the same thing and eventually figured it out if anyone else was curious here is an example
the â€śrotation[1]â€ť I have turns my prefab 180 degrees on the y-axis but can be changed to â€śrotation[0]â€ť for the x-axis or â€śrotation[2]â€ť for the z-axis.

``````        Quaternion rotation = new Quaternion();
rotation[1] = 180f;
Instantiate(KnightPrefab, new Vector3(12,7,0), rotation);
``````

@JamesLeeNZ If you are not going to help people why post at all? Most people know about documentation and/or some of it can be difficult for beginers.

5 Likes

This is plain wrong information though. Quaternions have 4 parts, x,y,z, and w, and they do not correspond directly to rotations around the x, y, z (w?) axes. For that youâ€™ll want to do Quaternion.Euler(x, y, z).

Your code would be accurately written as:

``````Quaternion rotation = Quaternion.Euler(0f, 180f, 0f);
Instantiate(KnightPrefab, new Vector3(12,7,0), rotation);
``````

Note this section of the Unity manual where it says not to use the x,y,z,w properties unless you understand the not-straightforward-at-all math behind them. https://docs.unity3d.com/ScriptReference/Quaternion.html

3 Likes

Interesting, just out of curiosity why is it that when I changed the values as i described i got that outcome?
For me it seems to work fine,whats the difference, can it cause a problem if I donâ€™t use the .Euler

just for clarification the way i read it is i just set a specific one and none of the others, opposed to setting all of them at once

It works fine because you got lucky and it happens to work for the specific value 180. Try any value besides 180 for your angle. It wonâ€™t work. In fact any value over 1 you put will give you a y rotation of 180 degrees. The values in a quaternion range from -1 to 1, and again, they donâ€™t map directly to axis rotations. Play around a little with a script like this on a cube in an empty scene and youâ€™ll see what I mean:

``````using UnityEngine;

public class QuaternionsDemo : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Quaternion q = new Quaternion();
q.y = .5f;
transform.rotation = q;
}
}
``````
1 Like

The way you read was about using Euler angles and avoiding gimbal lock. They are specifically talking about using .Euler in that case.

1 Like

Dang, thanks your right. Yeah if i was going for anything other then 180 it would have not worked i got lucky for sure. thanks for the help, your way works perfectly.

1 Like