I am having an issue with moving a game object using MoveTowards.

I am in the latter stages of development for my 2D platform game and am currently working on a boss battle. I have previously used Vector3.MoveTowards for various things such as enemies and moving platforms and have had no issues but for some reason the movement for this object is just not working and I can’t figure out why not. Every other part of the script works absolutely perfectly but the movement is just not happening. Could someone please help me to figure out what I must be missing? Scripts are added below.

First, the movement script

{
    public Vector3 posA;
    public Vector3 posB;
    public Vector3 posC;
    public float speed;
    [SerializeField]
    private Transform childTransform;
    public bool movingright = false;
    public bool movingleft = false;
    public Vector3 currentpos;
    public Vector3 nextpos;
    private SpriteRenderer sidsprite;
    public Animator sidanim;
    public Transform[] waypoints = new Transform[3];

    // Use this for initialization
    void Start ()
    {
        currentpos = waypoints[0].localPosition;
        nextpos = waypoints[1].localPosition;
        posA = waypoints[0].localPosition;
        posB = waypoints[1].localPosition;
        posC = waypoints[2].localPosition;
        sidsprite = GetComponentInChildren<SpriteRenderer>();
        sidanim = GetComponentInChildren<Animator>();
    }

    public void FindNext()
    {
        if (currentpos == posA)
        {
            nextpos = posB;
            Debug.Log("Next Position is " + nextpos);
        }
        if (currentpos == posB)
        {
            nextpos = posC;
            Debug.Log("Next Position is " + nextpos);
        }
        if (currentpos == posC)
        {
            nextpos = posA;
            Debug.Log("Next Position is " + nextpos);
        }
    }

    public void Flip()
    {
        if (nextpos == posA | nextpos == posB)
        {
            sidsprite.flipX = true;
            movingright = true;
        }
        else if (nextpos == posC)
        {
            sidsprite.flipX = false;
            movingleft = true;
        }
    }

    public IEnumerator Movements(Transform[] waypoints)
    {
        sidanim.SetBool("IsIdle", false);
        sidanim.SetBool("IsMoving", true);
        if (Vector3.Distance(currentpos, nextpos) >= 0.01f)
            childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextpos, speed * Time.deltaTime);
        if (Vector3.Distance(childTransform.localPosition, nextpos) < 0.01f)
        {
            movingright = false;
            movingleft = false;
            sidanim.SetBool("IsIdle", true);
            sidanim.SetBool("IsMoving", false);
            yield return new WaitForSeconds(1);
            FindNext();
        }
    } 
}

All of the relevant transforms and everything is added in the inspector and everything works apart from the character does not move. The animations happen and the wait times are correct and it finds the co-ordinates for the current and next positions as it should.

The movement is called through a separate script to activate the coroutine

public class StartBossRoutine : MonoBehaviour
{
    private SidMovement move;

	// Use this for initialization
	void Start ()
    {
        move = FindObjectOfType<SidMovement>();
	}
	
    public IEnumerator StartMove()
    {
        move.sidanim.SetBool("IsIdle", true);
        move.FindNext();
        yield return new WaitForSeconds(2);
        move.Flip();
        StartCoroutine(move.Movements(move.waypoints));
        Debug.Log("Should be moving");
    }
}

This also works fine and the sprite flips at the correct time and the debug prints that the character should be moving but HE DOESN’T!!

Any assistance would be greatly appreciated.

The answer is simply that your MoveTowards is only being called once. the MoveTowards function does not constantly move the Vector3’s toward each other without being called. Instead, each time the MoveTowards is called, it moves the vector closer to the target by the scale defined in the function. To fix this, just make it run the movetowards over and over until the object meets its target.

If this solved your problem, please accept my answer. If not, feel free to ask any more questions :slight_smile: