- Home /
Set rotation values equal to 0
I have an object that's rotated around the y-axis (let's say 0,30,0). Is there any way I can set these values equal to 0? So I don't want to rotate the object back to 0,0,0, but I want to set the rotation at 0,30,0 as a default rotation.
So in the script I want to set that the rotation values are now equal to 0,0,0 without changing the rotation/scale or position of the object.
Thank you very much!!!
Answer by _dns_ · Jan 04, 2015 at 04:03 PM
Hi, as far as I understand: you want to rotate your object without rotating the transform component. That is, only rotate the mesh (I assume that object contains a mesh). I see 3 options:
modify the mesh before importing it: any 3D editing software will do this
modify the mesh itself by a script
Insert a new Transform as the parent of this object
Option 3 should be the quickest way to do it: create an object containing only the mesh as a child of the main object. Change the rotation of this child mesh to (0,30,0). The parent object still have 0 rotation and can contain all the scripts you need, be referenced by other object etc...
Option 2 would take place in the Start() of the object. You could get the vertices of the mesh using the MeshFilter component and rotate them. Create a Matrix4x4 from a TRS with only a (0,30,0) rotation. Use it to rotate the vertices by multiplying the vertices positions by the matrix, and set the vertices back to the mesh. You should then recompute the normals and the bounds (or rotate the normals with the matrix). Your script should be close to this one.
Just to make sure that you understand what I mean, this is what I would like to have:
An object with rotation 0,30,0 When I click play, the script sets the rotation values to 0,0,0 but the object stays, it doesn't move or rotate When I tranform the rotation in the script to 0,30,0, the object rotates with 30 degrees from it's initial position (so this would be 0,60,0 if I didn't set the values to zero)
Thank you for your time and effort!
$$anonymous$$
Ok, that's what I understood :) The thing is when you write "but the object stay" : it makes no sense. The object cannot "stay", it is one with the transform's rotation. That's why object (= transform) hierarchy is convenient.
The option 3 I described is what you want: You have an object O that contains scripts but no mesh. You create an object G that only contains a mesh. You make G the child of O (with G's scale, position & rotation to 0).
Now, consider O's rotation is 0, and G's rotation is 0. Everything is "aligned" = default.
Rotate G by (0,30,0) in the inspector. You now have a rotated object in the scene, but O's rotation is still = to 0. That's your initial values.
Now, in game, if you want to rotate G, don't: rotate O ins$$anonymous$$d. O's rotation will be 0 when you start the game. If you rotate O by (0,30,0) it will rotate G by 30 too, then G will be rotated by 60.
That's what transform hierarchy is made for ;-)
Oh I understand I need it for a door opening system, and I have a door object that is child to a hinge. So when I rotate the hinge, the door opens/closes like a door ins$$anonymous$$d of just turning around it's middle axis. I figured out how I can achieve what I want thanks to your answer, tyvm!
Answer by flaviusxvii · Jan 04, 2015 at 03:05 PM
Add this line to the Start() function..
transform.rotation = Quaternion.Euler(0, 30, 0);
That will make it start with that rotation.
yes, I'll try that, but that will put the rotation of the object in the inspector to 0,30,0
I want that the values in the inspector show 0,0,0 but that the object is rotated 30 degrees around the y axis
Answer by malekbakeer · Jan 04, 2015 at 02:35 PM
// put the following in where u like
transform.rotation.y = 0;
yes, but won't that rotate the object when the script is running? I want the object to stay in place, but I want the value to change
Answer by Budlog · Jul 15, 2015 at 07:22 PM
try Space.World as a 4th paremeter of Rotate method.
transform.Rotate(0, angle * 8, 0,Space.World);
Answer by 123iamking · Feb 06, 2016 at 02:15 PM
I think you mean: YourGameObject.transform.localRotation = Quaternion.identity;
Your answer
Follow this Question
Related Questions
Store initial rotation in JavaScript 2 Answers
Problems caused by non-unique Euler angle solutions 1 Answer
Instantiating an object in front of the player 3 Answers
Camera Zoom from rotation. 0 Answers