# Cube rotating and moving

I am controlling my cube as a player. When I am rotating a cube it is not moving in the rotated direction. The script used is as follow :

``````void PlayerMove()
{
if(Input.GetKey(KeyCode.W))
transform.position += new Vector3(0,0,0.1f);

if(Input.GetKey(KeyCode.S))
transform.position -= new Vector3(0,0,0.1f);

if(Input.GetKey(KeyCode.A))
transform.Rotate(0,-1,0);

if(Input.GetKey(KeyCode.D))
transform.Rotate(0,1,0);

}
``````

I know i have to add something. But I am not sure what to add.

You have to transform.Translate() instead of transform.position because transform.position change the position of cube with respect to world coordinates, therefore its not move in the changed direction.

Try this :

``````float speed = 10f;
void Update()
{
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * Time.deltaTime * speed);

if(Input.GetKey(KeyCode.S))
transform.Translate( -1 * Vector3.forward * Time.deltaTime * speed);

if(Input.GetKey(KeyCode.A))
transform.Rotate(0,-1,0);

if(Input.GetKey(KeyCode.D))
transform.Rotate(0,1,0);
}
``````

The problem you have with your code, is that you always move in the world z-axis.
So even if you rotate the object will will still move in the worlds z-axis.

So what you can use instead is the translate function, and base it on the local axis of your object instead:

``````private Vector3 MovementVector = Vector3.zero;

Update()
{
yourobject.transform.Translate(MovementVector, Space.Self);
}

PlayerMove()
{
if(Input.GetKey(KeyCode.W) == true)
{
MovementVector.z = 1.0f;
}
else if(Input.GetKey(KeyCode.S) == true)
{
MovementVector.z = -1.0f;
}
else
{
MovementVector.z = 0.0f;
}

// Etc. etc.
}
``````

So in this simplified example, is that you translate your object with the local coordinates instead of the world coordinates.