- Home /
Rotation not working
Hello all. I'm trying to rotate an object through code as I would using the "rotate" tool in the editor, but for whatever reason, the object seems to keep rotating along the global axis (Whereas I need it in the same spot, just turned to the side).
I've used obj.transform.Rotate(), obj.transform.RotateAround(), obj.transform.RotateAroundLocal(), using both Vector3.Up and obj.transform.up as parameters, and I've had the same misfortune trying to manipulate the obj.transform.rotation, obj.transform.localRotation, obj.transform.eulerAngles, and obj.transform.localEulerAngles, all not working as desired: the object still appears to be rotating along the global Y axis, as opposed to the local. For reference, what I am trying to do is explained in the attached image. Any help would be greatly appreciated.
Is the object a child of another object? Is it modelled to be centralised at 0,0,0?
And what do you do in the editor to make it change the way you want?
Answer by lollornr · Feb 03, 2013 at 08:20 PM
Manipulating transorm.rotation is a bad idea because you have to work with a quaternion, not a Vector3. Maybe the origin of the object that you try to rotate isn't the origin of the geometry!
There's nothing wrong with working with Quaternions ;)
Answer by robertbu · Feb 03, 2013 at 07:58 PM
Are you sure about your diagrams? I would expect the arrow to point in the opposite direction on the results panel of "What is happening." If your diagram is correct, then you likely need to post some source code so that folks take a take a look.
Assume the arrow is backwards in the diagram, I recommend you replace the arrow with Unity's build-in cube object and then run your code. If the cube suffers the same problem, then the problem is likely a parent/child issue of some sort. If the cube does not suffer the same problem, then your issue is with the arrow object and likely a problem with centralization as @whydoitdoit suggested above. Note you can adjust for a model not being centralized by making the model a child and empty game object and adjusting for the offset.
Answer by sparkzbarca · Feb 04, 2013 at 08:37 AM
if your trying to rotate an object in the manner in which you would rotate a clock hand you dont want to rotate on the y axis you want to rotate on the forward axis.
if you'll notice with a clock the axis of rotation is the pin that goes through the middle of it. rotating is a twisting action along an axis. So if you think of a clock with the pin you need that pin axis.
Your answer
Follow this Question
Related Questions
Rotation Jumping values (0 to 180) 1 Answer
How to instantiate on custom rotation? 1 Answer
Rotate object to where aiming 1 Answer
Player character X rotation going haywire on game start 1 Answer