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 :stuck_out_tongue: I’m pretty frustated, since this should be an easy excercise and I’m having so much problems with it…

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;