c# movement

Hi.
I want my vehicles to move 10 meters away from where the vehicle is located in the z(I don’t want the vehicle to move in the X and Y too) and than return to the place he started.
I have multiple vehicles.

You can look into this tutorial for several options and demonstrations on movement:

This has examples of move to a position by speed or time.

This isn’t everything, but it’s a start to move to a location with a set speed:

public float speed=5;
public float minDistance = 1;
Vector3 targetPosition;
Vector3 originalPosition;

void Start()
{
    originalPosition = transform.position;
    targetPosition =   new Vector3(transform.position.x, transform.position.y, transform.position.z + 10);
}

void Update()
{    
    transform.position = Vector3.MoveTowards(transform.position,  targetPosition, speed * Time.deltaTime);

if(Vector3.Distance(transform.position, targetPosition <= minDistance )
{
       //reached destination, switch targetposition with originalposition
}

Are you saying that you only want the vehicle to move back and forth just once? If it’s repeated, my code here will work. Not sure if you are asking about how to handle collisions. Do let us know…

using UnityEngine;

public class Oscillate : MonoBehaviour
{
    [SerializeField] float movespeed = 5;
    Vector3 startPosition;
    Vector3 endPosition;
    Vector3 targetPosition;

    void Start()
    {
        startPosition = transform.position;
        endPosition = startPosition + new Vector3(0, 0, 10);
        targetPosition = endPosition;
    }

    void Update()
    {
        if ((transform.position - targetPosition).magnitude < 0.1)
        {
            if (targetPosition == endPosition)
            {
                targetPosition = startPosition;
            }
            else
            {
                targetPosition = endPosition;
            }
        }
        float move = movespeed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, move);
    }
}