I’m trying to make a rectangle turn and move, but when I move it, it moves in relation to the initial starting rotation, as opposed to the actual rotation of the object. Basically, if I rotate it 90 degrees to the right, it will still move around as if I had not done any rotation. I’m relatively new to Unity, so apologies if this is a really stupid question.
Here’s my code for movement (in C#):
if(Input.GetKey(KeyCode.W)) moveDirection.x -= 1; if(Input.GetKey(KeyCode.S)) moveDirection.x += 1; if(Input.GetKey(KeyCode.Space)) moveDirection.y += 1; if(Input.GetKey(KeyCode.LeftAlt)) moveDirection.y -= 1; if(Input.GetKey(KeyCode.D)) transform.Rotate(Vector3.up * Time.deltaTime); newPosition = moveDirection * (moveSpeed * Time.deltaTime); newPosition = transform.position + newPosition; transform.position = newPosition;
Where newPosition and moveDirection are vector3 variables, and moveSpeed is an integer variable.
What I want to happen is have the object this script is attached to move in relation to the current rotation, so if we were to look at this from a top-down, bird’s eye view, the box would move to the right if I rotated it 90 degrees so that it’s facing east, it would travel to the east, or if I rotated it so that it’s facing northwest, it would travel northwest, when the move forward key (W) is pressed.
As far as controls go, I’m using WSAD for forwards, backwards, left rotation, and right rotation, respectively.