- Home /
How do I rotate a game object and have it move in relation to the rotation angle?
Hello,
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.
I'd prefer to get help in the form of C# code, but I can probably translate anything provided in Javascript effectively. Thanks!
Answer by ProudOne · Jul 08, 2012 at 11:27 AM
Use transform.forward. That's the normalized Z-Axis vector of your current object.
Meaning; If you wanted to move forward by a meter:
transform.position = Input.GetAxis("Vertical") * transform.forward;
Hope that helps.
That seems to get the object to move in the correct direction, but like you said, only by one meter. The object then goes back to where it started when I release the key. How can I have continuous movement (where the object continues to move in the desired direction until the key is let go, at which point it stops)?
EDIT: Looks like this code disables my object's vertical movement, as well. For reference, I replaced "transform.position = newPosition;" with "transform.position = Input.GetAxis("Vertical") * transform.forward;"
Ah, you naturally need to use += when requiring continuous motion. transform.position += Input...
Also, if you need the Object to travel anywhere else but forward, transform has up and other directional components, too. So if you wanted to travel along the Y-Axis, use transform.up.
That worked, thanks! I'll post my working code as an answer for anyone else having this problem.
how to i do the same thing but ins$$anonymous$$d using animation window i mean creating animation clip
Answer by TRON-dll · Jul 08, 2012 at 07:02 PM
What I used (C#):
//Movement
//Move forwards and backwards
if(Input.GetKey(KeyCode.W))
moveDirection.x -= 1;
if(Input.GetKey(KeyCode.S))
moveDirection.x += 1;
//Move up and down
if(Input.GetKey(KeyCode.E))
moveDirection.y += .5f;
if(Input.GetKey(KeyCode.Q))
moveDirection.y -= .5f;
//Turn left and right
if(Input.GetKey(KeyCode.D))
gameObject.transform.Rotate(0, 90 * Time.deltaTime, 0);
if(Input.GetKey(KeyCode.A))
gameObject.transform.Rotate(0, -90 * Time.deltaTime, 0);
//Update position
transform.position += moveDirection.x * transform.right;
transform.position += moveDirection.y * transform.up;
transform.right was used instead of transform.forward because of the orientation of the model I was using.
moveDirection is a Vector3 declared within the update function. If anyone has questions or suggestions, do ask.