Moving from point A to B and back in a continuous loop?

Hi all,

I have a Vector3.MoveTowards Script, moving from point a to point B. I would like the object to move from point A to B and back in a continuous loop.

Any ideas?

Again, any help is much appreciated :slight_smile:

Kearley x

using UnityEngine;
using System.Collections;

public class MoveBackAndForth : MonoBehaviour
{
    public Transform pointA;
    public Transform pointB;
    public float speed = 10f;

    IEnumerator Start()
    {
        while (true)
        {
            do yield return null; while (MoveTowards(pointA));
            do yield return null; while (MoveTowards(pointB));
        }
    }

    bool MoveTowards(Transform target)
    {
        transform.position = Vector3.MoveTowards(
            transform.position,
            target.position,
            speed * Time.deltaTime);
        return transform.position != target.position;
    }
}

ok since like you havent given us much to go on here , look up mathf.pingpong in unity docs .
thats basically what you need.