- Home /
Setting value of transform.rotation in code, C#
If I wanted to change the transform.position of an object i'd do this:
Vector3 temp = transform.position;
temp.x = 7f;
transform.position = temp;
I want to do the same thing with transform.rotation, but I don't know what to replace the Vector3 with.
Answer by KellyThomas · Dec 28, 2013 at 05:25 PM
A Transfom
will store rotation as a `Quaternion`, great for 3D maths but bad for human brains.
One trick you can do is to convert to euler angles (similar to the rotation as displayed in the Inspector):
Vector3 temp = transform.rotation.eulerAngles;
temp.x = 90.0f;
transform.rotation = Quaternion.Euler(temp);
Ech. This is why i hate C# sometimes. So over complicated for such a simple task
Um what? WTF
In JS it's just
var temp = transform.rotation.eulerAngles;
temp.x = 90.0f;
transform.rotation = Quaternion.Euler(temp);
And you can write "var" in C#.
Quaternion.Euler isn't a C# thing, it's a UNITY thing!
Answer by LukeNukem44 · Jun 27, 2018 at 12:30 PM
Rotate your character naturally:
transform.Rotate(x, y, z); // Local
transform.Rotate(x, y, z, Space.World); // Global
In case you want to rotate to an absolute angle:
transform.rotation = Quaternion.Euler(x ,y ,z);
There's a lot more to research in the manual.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
RTS Camera movement wrong after rotation 1 Answer
slowly rotate a object *need quick fix* 0 Answers
transform.Rotate is rotating all axes, why? 0 Answers
Position in Vector (C#) 1 Answer