# Moving an object via local transform

I didn’t think this question was directly related to my other, because it is about the movement.

I have this script

``````function MoveForward()
{
var originalPosition = transform.localPosition.x;
var newPosition = transform.localPosition.x + gridSize;
time = 0.0;
while (time < 1.0)
{
time += Time.deltaTime * (moveSpeed/gridSize);
transform.localPosition.x = Mathf.Lerp(originalPosition, newPosition, time);
yield;

}
canMove = true;
}
``````

Which works to an extent. I push up (checks in update) and it runs this function and the object moves a specified distance (gridsize).

However, it only moves along the x axis when I want it to move in the direction the object is facing (which I assumed would be the local x axis). I have two other functions which rotate the object left and right, and I got them working (but not in the way I wanted, but it works). However, I assumed that transform.localPosition.x would refer mean it would transform it along the local coordinates, like the gizmo does when you enable local in the scene view. Apparently not.

Is there a way to transform the object along it’s local transform, rather than the world position. Not sure if I am using the correct terminology.

You want

transform.forward

Which is a unit vector (vector of length 1 unit) in the “forward” direction based on the transform’s orientation. Assuming that matches your model’s forward.

1 Like

Got it

``````function MoveForward()
{
var originalPosition = transform.position;
var newPosition = transform.position - transform.forward;
time = 0.0;
while (time < 1.0)
{
time += Time.deltaTime * (moveSpeed/gridSize);
transform.position = Vector3.Lerp(originalPosition, newPosition, time);
yield;

}
canMove = true;
}
``````

Cheers! works perfectly!

1 Like

you can also move your gameObject relative to other objects spaces using the translate function.