# Make movement adjust with rotation(player coordinates vs world coordinates)

I threw together this simple movement code but when I rotate my player object it doesn’t take the rotation into account. Is there some way to make the vector3s correspond to the player transform instead of the world transform

`````` if (Input.GetKeyUp(KeyCode.W))
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
if (Input.GetKeyUp(KeyCode.S))
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
if (Input.GetKeyUp(KeyCode.A))
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
if (Input.GetKeyUp(KeyCode.D))
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
if (Input.GetKey(KeyCode.W))
{
}
if (Input.GetKey(KeyCode.S))
{
}
if (Input.GetKey(KeyCode.A))
{
}
if (Input.GetKey(KeyCode.D))
{
}
``````

I swapped my rb.AddForce to use transform.forward instead of vector3.forward and that fixed the movement but the code to sop the player when you et go is broken because of that

Ok. So post your new code.

I just replaced Vector3.direction with transform.direction

`````` if (Input.GetKeyUp(KeyCode.W))
{
//transform.forward = new Vector3(0, 0, 0);
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
if (Input.GetKeyUp(KeyCode.S))
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
if (Input.GetKeyUp(KeyCode.A))
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
if (Input.GetKeyUp(KeyCode.D))
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
if (Input.GetKey(KeyCode.W))
{
}
if (Input.GetKey(KeyCode.S))
{
}
if (Input.GetKey(KeyCode.A))
{
}
if (Input.GetKey(KeyCode.D))
{
}
``````

So you know, reading a property like “velocity” means Unity has to read from the C++ runtime engine that value which if you’re doing it lots of times means wasted effort. This is no different than getting a component reference once then using it.

Just read it once and cache it in your method then manipulate that i.e.

``````var vel = rb.velocity;
...
rb.velocity = new Vector3(vel.x, vel.y, 0.0f);
``````

A few here and there doesn’t make much difference but if you continue doing it like this all over the place then it adds up.

Ok thanks, I will do that. Any suggestions for solving my original issue?

I tried changing my stopping code to use turn the velocity vector from world space to local space but that just changed from doing the wrong thing to doing nothing

``````if (Input.GetKeyUp(KeyCode.W))
{
//transform.forward = new Vector3(0, 0, 0);
rb.velocity = transform.InverseTransformDirection(new Vector3(rb.velocity.x, rb.velocity.y, 0));
Debug.DrawRay(transform.position, rb.velocity, Color.red, 5);
}
if (Input.GetKeyUp(KeyCode.S))
{
rb.velocity = transform.InverseTransformDirection(new Vector3(rb.velocity.x, rb.velocity.y, 0));
Debug.DrawRay(transform.position, rb.velocity, Color.red, 5);
}
if (Input.GetKeyUp(KeyCode.A))
{
rb.velocity = transform.InverseTransformDirection(new Vector3(0, rb.velocity.y, rb.velocity.z));
Debug.DrawRay(transform.position, rb.velocity, Color.blue, 5);
}
if (Input.GetKeyUp(KeyCode.D))
{
rb.velocity = transform.InverseTransformDirection(new Vector3(0, rb.velocity.y, rb.velocity.z));
Debug.DrawRay(transform.position, rb.velocity, Color.blue, 5);
}
``````

When using WADS controls it isn’t necessary to read each individual key. The WADs keys are mapped to Input.GetAxis() which can be passed directly to AddRelativeForce.

``````rb.AddRelativeForce(new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"))*speed);
``````

You can’t use GetKeyUp or GetKeyDown from FixedUpdate and so you shouldn’t try to use those functions to stop your player. You can let the physics engine stop your player with friction, or you can increase the drag setting on the rigidbody.

Or if you want your script to stop the player then you can adjust the input force using the current velocity.

``````force-=rb.velocity*0.3f;
``````

``````using UnityEngine;
public class SimpleRigidbodyController : MonoBehaviour
{
Rigidbody rb;

void Start()
{
rb=GetComponent<Rigidbody>();
rb.freezeRotation=true;
}

void Update()
{
rb.MoveRotation(Quaternion.LookRotation(transform.forward+(Vector3.up*Input.GetAxis("Mouse Y")+Input.GetAxis("Mouse X")*transform.right)*0.05f)); // look around using the mouse
}

void FixedUpdate()
{
Vector3 force=((transform.right*Input.GetAxisRaw("Horizontal"))+(transform.forward*Input.GetAxisRaw("Vertical"))).normalized; // Get the world space move direction
if (Physics.SphereCast(transform.position,0.5f,-transform.up,out RaycastHit hit,0.55f))  // are we grounded?
{
force=Vector3.ProjectOnPlane(force,hit.normal)*5; // align input force to the ground and increase the force
force-=rb.velocity*0.3f;   // This will prevent us from moving too fast and resist sliding around
if (Input.GetKey(KeyCode.LeftShift)) force*=2;  // sprinter