- Home /
Object only rotates in local space
I am trying to get an object to rotate on the Y axis in world space. The issue seems to be that the object rotates fine (maybe on its local axis), but the global rotation is unchanged. Can someone point out where I am going wrong here?
function Update () {
if (inputLeft) {
transform.Rotate(Vector3.up, -rotateSpeed * Time.deltaTime, Space.World);
}
}
The code above gives me this :
Do you have the Gizmo set to Local in the toolbar?
I get the same result regardless here, thanks though.
Answer by Juice-Tin · Jun 28, 2014 at 06:51 PM
You may confused.
For a car you would want a local rotation, this way it stays forward facing.
Either that, or the script is perhaps on a child object of the car when it should be on the top-most object?
It's hard to tell really, but I would say experiment with using localRotation, or just omitting that Space.World.
Also have a look at Wheel Colliders if you want more realistic car movement/control.
Thanks, Juice-Tin. You are right. I want the car to stay forward facing. The issue I am having is the car is rotating, but I can't use transform.forward because it is referring to the world direction.
r6834, I'm rather new to Unity as well, but just looked back on my game that uses similar code for the Camera. $$anonymous$$y camera's arrows in the editor stay facing the same direction as yours does, however it uses .forward just fine.
Have you tried using forward while adjusting your code? Or have you just been stuck on getting the editor arrows to adjust with the car? (Since forward works on $$anonymous$$e, but arrows stay the same).
Also, try using local rotation if you haven't yet.
Tried it, all I am simply trying to do is rotate the vehicle with the script above, then add a forward force to the Rigidbody. But, it refuses to move in any direction but the global directions.
rigidbody.AddForce (transform.forward * inputVertical);
Are these scripts on the same transform, or is the car childed to a parent?
Answer by Bunny83 · Jun 30, 2014 at 03:10 AM
There are only two possible reasons why your gizmos keep the world space direction:
Because like alucardj said you have set the gizmos to global instead of local. However that won't influence transform.forward
This script is attached to a child object (which is rotating) but you have selected the parent object which of course isn't rotated.
Make sure your controlling scripts (rotation / movement) are attached to the top most object and not to some child object.