# Location and Rotation not Working

I'm simply trying to move an object on the local Z axis with the W and S keys, so I used transform.localPosition instead of transform.position which gave me the same result :/. The other problem is the rotation which only rotates for like 3 seconds then stops why doesn't this move my object on the local axis and what is wrong with the rotation?

``````var speed : float = 0.1;

function Update(){
//forward
if(Input.GetKey("w")){
transform.localPosition.z += speed;
}
//backward
if(Input.GetKey("s")){
transform.localPosition.z += -speed;
}
//left
if(Input.GetKey("a")){
transform.rotation.y += -speed;
}
//right
if(Input.GetKey("d")){
transform.rotation.y += speed;
}
}

``````

Thanks

transform.localPosition.z is meant to be read only or for specific placement of an object, not for translation. You're going to get very inconsistent results doing it the way you've shown above.

What you want is transform.Translate (0,0, speed*Time.deltaTime) Just change your speed variable from + to - to change the direction. There are good examples in the scripting reference.

Same thing for rotation: transform.Rotate (X,Y,Z);

The following is directly from the scripting reference:

``````// A very simplistic car driving on the x-z plane.

var speed : float = 10.0;
var rotationSpeed : float = 100.0;

function Update () {
// Get the horizontal and vertical axis.
// By default they are mapped to the arrow keys.
// The value is in the range -1 to 1
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;

// Make it move 10 meters per second instead of 10 meters per frame...
translation *= Time.deltaTime;
rotation *= Time.deltaTime;

// Move translation along the object's z-axis
transform.Translate (0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
}

``````

Two things:

First rotation or localRotation are Quaternions but you used them like eulerAngles.

As tool55 mentioned donâ€™t increase eulerAngles because setting the angles to wrong values will fail. To rotate a transform use a Quaternion or what will be easier in your case (also like tool55 said) the Rotate function

``````transform.Rotate(0,0,rotationSpeed * Time.deltaTime);

``````

rotationSpeed is in degrees per sec.

If your object isn't a child localPosition equals position. That means it's the position in world space and increasing z will move it on the global z axis. If it is a child position.z will represent the position in the local space of your parent but not in your own local space.

To move along the own local-z-axis you can use transform.forward which is a vector that points in the z axis of your transform. Just use that vector and add it to your position like:

``````transform.position += transform.forward * moveSpeed + Time.deltaTime;

``````

moveSpeed is in Units per sec. Which should be meters.