- Home /
Rotating a cube changes its local forward?
I've got a Cube transform, and I've created my first script to make it move and rotate on key press. The script basically does this on Update():
if (Input.GetKey(KeyCode.W))
{
thisTransform.Translate(thisTransform.forward * Speed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S))
{
thisTransform.Translate(thisTransform.forward * Speed * -1f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
thisTransform.Rotate(thisTransform.up * Speed * -10f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
thisTransform.Rotate(thisTransform.up * Speed * 10f * Time.deltaTime);
}
I think this should work, but when I run the scene, it actually moves and rotates, but as soon as I rotate the Cube it begins moving slighly sideways to the right. And if I keep rotating it'll then go backwards, and then sideways to the left, and forward again and so on!
It's like the Cube's forward vector is being rotated as well. If I rotate the Cube by 90 degrees, its forward vector seems to go another 90º off its previous front-facing side, so the cube starts moving sideways (that is, its front-facing side looking at 90º from its initial position, but moving to the exact opposite direction it was facing initially...).
Sorry if my explanation is a bit confusing :P I'm pretty frustated, since this should be an easy excercise and I'm having so much problems with it...
it's possible RotateAround can help you - one of the most useful calls in Unity.
I actually considered doing that, RotateAround self or something like that :P Is there such a thing?
Answer by robertbu · Oct 09, 2013 at 02:58 PM
Your problem is that Transform.Translate() uses local direction and you are supplying it a world direction. That is thisTransform.forward is the world forward. Vector3.forward will be the local forward. So to fix your problem, you can do one of:
thisTransform.Translate(Vector3.forward * Speed * Time.deltaTime);
or:
thisTransform.Translate(thisTransform.forward * Speed * Time.deltaTime, Space.World);
or:
thisTransform.position += thisTransform.forward * speed * Time.deltaTime;
It works!
So, if I understand correctly, the problem is that thisTransform.forward returns a world direction and Translate expects a local direction unless you specify otherwise with the Space.World argument, right?
I thought Vector3.forward returned a global direction, but it seems to return the local forward vector... Weird, my $$anonymous$$cher's papers must be outdated or something.
Vector3.forward is (0,0,1) can be either local or world. In a world context, it points directly towards positive 'Z'. Think about local coordinates. They are relative to the object. It is like your personal coordinate system. Your left is always your left, and your forward is your forward no matter what way you are facing. The origin of the coordinate systems is the pivot point of the object, and the x, y, and z axes are aligned with the object. So local Vector3.forward will always be the forward of the object no matter what way the object is facing.
I see, I get it. It's just that I thought the default context to be World, and not local! I'll keep it in $$anonymous$$d from now on :)
it's worth realising that transform.forward (also right, etc) are utterly different things from Vector3.forward
Yes, I see it now. Vector3.forward just translates to Vector3(0, 0, 1), so it contains no information about anything local nor global!
Your answer
![](https://koobas.hobune.stream/wayback/20220613115615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Porting to Windows: Translate problem 2 Answers
Walking forward 2 Answers
Trying to make a Projectile Rotate and Move Forward.. 1 Answer
rotation in dragging 2 Answers