Slide object between two moving points c#

Hi everyone,

sorry for my English, I’m trying to learn it by myself.

I need help to create a c# script to make slide an object between two moving points.

PointA and PointB have attached a script which move them randomly in the scene.

In the image linked below in step 1 you can see the point A, the point B and the spawned object in the starting situation

Image and video hosting by TinyPic

In step two in the image linked above you can see my current situation whit the script below which is attached to spawned object:

private Transform pointA;
private Transform pointB;

void Update()
	{
		pointB = GameObject.FindWithTag ("PointB").GetComponent<Transform> (); //Constant search of pointB's position in the scene
		float speed = -0.1f * Time.deltaTime; 
		transform.position  =  pointA.position + (poinA.position - pointB.position) * speed;
}

If the pointB are moving the spawned object doesn’t remains on the line between the two points.

In step 3 in the image you can see what I want: I need that the spawned object face to point B and constantly remains on the line between the two points while moving to poinB.

EDIT: I forgotten to say that the distance between the points is not always the same.

Thank you for any help you give me.

  1. Don’t use GameObject.FindWithTag ("PointB").GetComponent<Transform> (); in the Update() function. Put it in start or make public Transform variables and drag them in the editor on the script

2)Use FixedUpdate() to prevent jitter

  1. Use Vector3.Lerp for a smooth movement.
    Example Code:

    public class slide : MonoBehaviour {

     public Transform pointA, pointB;
     float speed;
    
     void FixedUpdate ()
     {
         speed =10*Time.deltaTime;
         transform.position = Vector3.Lerp(pointA.position, pointB.position, speed);
    
     }
    

    By using point A as destination and pointB as target, you ensure that your spawned object remains in one line between the two.