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