Why MoveTowards not working in place of LERP

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Move : MonoBehaviour
{

    [SerializeField] List<waypoints> path = new List<waypoints>();

    [SerializeField] float speed;

    // Start is called before the first frame update

    void Start()

    {

        StartCoroutine(Path());

    }

    IEnumerator Path()

    {

        foreach (waypoints waypoints in path)

        {

            transform.LookAt(waypoints.transform.position);

            Vector3 startpoint = transform.position;

            Vector3 endpoint = waypoints.transform.position;

            float delta = 0;

            while (delta < 1f)

            {

                delta += Time.deltaTime*speed;

                transform.position = Vector3.MoveTowards(startpoint,endpoint,delta*speed);

                yield return null;

            }

        }

    }

}

Hi Ashu47,

Did you test your idea? Does it work? If so, it’s a solution. :slight_smile:


See also:

It’s not working properly.
It moves some distance and stop.

What distance? Without knowing what you did exactly, I’m afraid I cannot tell you what might have gone wrong in your case. Theoretically, the Lerp method should have worked too.

Vector3.MoveTowards does not work like a Lerp. You are using MoveTowards but you are giving it Lerp parameters.

The first parameter should be the current position, i.e transform.position. You are giving it the startpoint . The last parameter is the max amount it may move this frame. You are adjusting that value but the closer it gets to the endpoint, the less it will move because it’s calculating the distance from the startpoint.

Essentially, you are telling it to move from point a to point b in increments of c. If a = 0, b = 10 and c = 1 it will only move 1 step. This is because it returns the position if it took c steps from a. Which is always a + c.

Btw., you are also multiplying speed twice, so your speed may not be what you expect.

What you want to do is this. Note that speed here is now how many units you want to move per second.

transform.position = Vector3.MoveTowards(transform.position, endpoint, speed * Time.deltaTime);

This will also keep the speed consistent whether you’re travelling 2 units, or travelling 20 units.

You’d want to terminate the loop (while (delta < 1f)) when the transform.position is at (close to) the endpoint. Something like this

while (Mathf.Approximately(Vector3.Distance(transform.position, endpoint), 0))
{
    transform.position = Vector3.MoveTowards(transform.position, endpoint, speed * Time.deltaTime);
}

// or

while (true)
{
    transform.position = Vector3.MoveTowards(transform.position, endpoint, speed * Time.deltaTime);
    if (Mathf.Approximately(Vector3.Distance(transform.position, endpoint), 0))
    {
        break;
    }
}

Thanks’ bro for guidance. I done some changes in my code and now it’s working.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Move : MonoBehaviour
{
    [SerializeField] List<waypoints> path = new List<waypoints>();
    [SerializeField] float speed;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Path());
    }
    IEnumerator Path()
    {
        yield return new WaitForSeconds(1);
        foreach (waypoints waypoints in path)
        {
            Vector3 endpoint = waypoints.transform.position;
            transform.LookAt(endpoint);
            float distance = Vector3.Distance(transform.position, endpoint);
            while (transform.position != endpoint)
            {
                transform.position = Vector3.MoveTowards(transform.position, endpoint, distance * Time.deltaTime*speed);
                yield return null;
            }
        }
    }
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms