How do I create a simple follow script?

Sorry, I’ve used the search function and I can’t find exactly what I’m looking for.

I’m trying to make a very simple AI follow and they should rotate on the path, but it’s really glitchy and my model glitches out a lot…

Here’s my current script:

void Chase()
{
    playerPosition = player.transform.position;

    if (enemySight.playerInSight)
    {
        previousSighting = playerPosition;
        currentTarget = previousSighting;

        seeker.StartPath(transform.position, currentTarget, OnPathComplete);
    }


    if (path == null)
    {
        return;
    }

    if (currentWaypoint >= path.vectorPath.Count)
    {
        return;
    }
    Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;

    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * 8f);

    dir *= speed * Time.deltaTime;

    controller.SimpleMove(dir);

    if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
    {
        currentWaypoint++;
        return;
    }
}

public void OnPathComplete(Path p)
{
    Debug.Log("Any error? " + p.error);
    if (!p.error)
    {
        path = p;
        currentWaypoint = 0;
    }
}

My problem is that my enemy AI will “see” the player and then it will start to glitch out, because I have set it to rotate to look towards the path. It seems to be always calculating the path and then trying to rotate towards the new paths everytime it “recalculates” the path.

How can I make it “follow” the player continuously as long as one of my bools is true, but not break it up into several different paths? (Like don’t use path.isDone() because I want it to actually update the path if it sees the player moving somewhere else, but still in its vision…)

I have also noticed that BECAUSE I have more enemies instantiated, the game lags more and then the physics/enemies glitch out more… I haven’t had a problem with 100s of this model instantiated before, so I don’t know if it’s A*'s fault or something else…

I’m using Astar free, btw.

Hi

Have you tried using one of the included movement scripts such as the AIPath script?