Make object go 2 steps forward in period of time

I know this sounds like a question which has been asked before. But i cant find any answers on the net, so know im asking! How do i make my object move 2 on the z-axis over a period of time
if i press space?

You just didn’t search enough.

Vector3 startPos;
Vector3 endPos;
float timer = 0;
float seconds = 8; // The total time you want the movement to take.

void Start()
{
   // Initialize startPos and endPos here.
   startPos = new Vector3(0, 0, 0);
   endPos = new Vector3(startPos.x, startPos.y, startPos.z + 2);
}

void Update()
{
   timer += Time.deltaTime / seconds;
   transform.position = Vector3.Lerp(startPos, endPos, timer);
}

Now this will move the object as soon as you run the game. You can easily modify it so that it moves only when the space is pressed. Or you can make it so that it will move to the endPos once you press space. I don’t know how you want it to be so I leave that to you.