Moving an object to a random position between two objects


I have a set of enemies currently moving to a set object. This object is on the far left of my game, and another object is at the far left. How could I make the enemies individually move to a random spot between these objects? This is my current code for their movement;

 void Update() {
          transform.position = Vector3.Lerp(startingPos, endingPos,
          Mathf.SmoothStep(0f,1f, Time.time/secondsForOneLength));

Also, for some reason while using the above code my collider isn’t applying the AddForce I’ve set to it. If I remove the code for their movement, it’ll work just fine.

Here’s the code for my collider;

void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.tag == "Enemy") {
        other.GetComponent<Rigidbody2D>().AddForce(new Vector2(speed, speed));            

Been struggling with this for some hours now. Any help much appreciated!

Suppose your two points are p0 and p1, use this:

Vector3 v = p1 - p0;
Vector3 target_position = p0 + Random.value * v;

v is the vector that goes from p0 to p1. By multiplying that vector with a value between 0 and 1 you choose a random point between p0 and p1.