Object movement - undesired effects

Hello everyone!
I’m new to unity and i’m trying to realize a simple thing: i want to make an object move from point A (a Vector3) to point B (another Vector3 but with a random x value), then when it reaches point B, i would like it to start moving left and right. I’ve created this code, but it generates two undesired effects:

  1. If the values of the random range of the x axis are not set from 0 to 1 (which is a single value) the object in his movement from point A to point B doesn’t follow a linear trail, it flickers rapidly and seems to move in various directions at the same time.

  2. When the objects reaches point B (the value 0 on the z axis) it is rapidly moved some units to the right, then it starts the correct “PingPong” movement. Can anyone help me?

    var enemySpeed : float;
    var start : Vector3;
    var speed : float;

    function Update () {
    transform.position = Vector3.Lerp(start, Vector3(Random.Range(-4,4),0,0), Time.time*speed);
    if (transform.position.z == 0){
    transform.position.x = Mathf.PingPong(Time.time * enemySpeed, 9);
    transform.position.z = Mathf.PingPong(Time.time * enemySpeed, 1);
    }
    }

In another script i have this line of code that instantiates the object, maybe it can help solving the problem:

Instantiate(enemies[0], Vector3(0,0,15), transform.rotation);

The values of this Vector3 and the “start” Vector3 variable are the same.

You are allocating a new random value every Update (every frame) - you need to choose the position to Lerp to in Start and then use it in update. It strikes me that your ping pong is starting from a fixed position so there will be a big jump when it kicks in. You probably want to add the ping pong to the position that was chosen for the Lerp.