[SOLVED]How to change Quaternion by 180 degrees?

I’m trying to instantiate an object, a bullet, so it comes from the back of the object. In other words, Im trying to change its degrees by 180, but it’s quaternion so it’s more complicated. Anyway here’s my code:

Instantiate(bullet, transform.position, transform.rotation);
//transform.rotation needs to be translated by 180 degrees

here’s the final working code:

			var rot : Vector3 = transform.rotation.eulerAngles;
			rot = new Vector3(rot.x, rot.y, rot.z + 180);
			Instantiate(bullet, transform.position, Quaternion.Euler(rot));

You can use Quaternion.eulerAngles to get a Vector3 representation of the Quaternion and then convert the Vector3 back to a Quaternion using Quaternion.Euler.

Psuedo code:

Vector3 rot = myTransform.rotation.eulerAngles;
rot = new Vector3(rot.x,rot.y+180,rot.z);
myTransform.rotation = Quaternion.Euler(rot);

Note that using euler representations of quaternion data can yield unexpected results (see: gimbal lock), so it is best to have clean data going in.

Hope that helps.

==

I find simply multiplying the value to work better and more readable.

// multiply rotation by 180 on y axis
transform.rotation *= Quaternion.Euler(0,180f,0);

Thank you this worked great.