Add one on local z axis

I wan’t to add one to my players local z axis… Like:

void Start(){
    Vector3 updatedPos = transform.localPosition;
    updatesPos.z += 1;
    transform.position = updatedPos;

}

This adds one on the z axis for the whole world (Can’t remember what its called… opposite of local)

What is the difference between this line…

Vector3 updatedPos = transform.localPosition;

and this line?

transform.position = updatedPos;

Answer that question and you will know what is wrong with your code.

Vector3s are immutable; you can’t actually change their values. This is due to how they’re implemented (as structs; ie. Value Types).

Try the following:

  //Shorten the name
  var locPos = transform.localPosition;
  Vector3 updatedPos = new Vector3(locPos.x, locPos.y, locPos.z + 1);

EDIT:

Oops, the question was actually how to move along the object’s z-axis, not to move it in local space.

The solution was to use the transform.Translate() method.

transform.localPosition += new Vector3(0f,0f,1f);