- Home /
[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));
Never use quaternions, unless you are doing something very advanced.
Just use Euler angles, or simply .Rotate.
Also LookAt is very useful.
Answer by equalsequals · Jun 17, 2013 at 05:27 PM
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.
==
Okay now I tried it and it worked thanks a lot!
if someone is having the same issue, here'e my final code: edit: I added it to the main question.
Answer by ikriz · Mar 13, 2014 at 04:43 PM
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);
Your answer
Follow this Question
Related Questions
Ellipse Animation by code 2 Answers
Sprint position for gun 1 Answer
w in Quaternion? 1 Answer
Bone Rotation on X (or any) axis Issues 1 Answer
Unity Rotate Raycast on Quaternion 1 Answer