C# Unity.AI chase moving target.Optimization.

Hi,
I am having a major problem figuring out how to optimize the way this follow function coordinates itself to the target. As of so far, I haven’t found anything to help from multiple sources. So, as a last effort to fix this frame issue, I turn to you, the lawful community.
The end game of this is for me to create a multi unit controller AI. As of right now it is privately owned by object.
Sorry in advance if there is something vital missing, I had to re-compile the script due to debugging notes.

Game runs at 100++FPS on average
Enemies on Patrol avg 120-350 FPS peaks at 500 FPS
Transition from Patrol to Follow, frames drop to 10-30 FPS, which visually bugs the game for roughly 1-3 seconds.
Once on Follow steady 50-100 FPS peaking to 150.

The problem is the Follow, FixedUpdate bring issues with our FPScontroller, so I implemented NavMesh.pathfindingIterationsPerFrame=10 (anything under, will lag pathfinding) I also would not like to use enumerator.

This is example of whole spread.
Patrol is done similar to Follow, but they are agent.setDestination random.range(0, waypoints.length) which work beautifully.
Thanks. From us to you.

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

public class PCTNavi : MonoBehaviour
{
public GameObject player;
public GameObject[ ] waypoints;
private Transform Target;

private int Point = 1;
private transform target;
private NavMeshAgent agent ;

void Follow() //this is the problematic frame dropper,
{

agent.SetDestination ( Target.transform.position );
NavMesh.pathfindingIterationsPerFrame = 10; // this brings steady frames after initialization of Follow(); and is at lowest.

Debug.Log(“11111”); //just to get informed

}

void Patrol()
{
agent.Resume();
int Index = Random.Range(0, waypoint.Length);
if (Point > 0)
{
agent.SetDestination ( waypoint [ Index ] . transform.position );
Point++;
Debug.Log(“22”); //just to get informed
}
if ( gameObject.transform == ( waypoint [ Index ]) || agent.remainingDistance < 0.5f )
{
Point = 0;
agent.SetDestination ( waypoint [ Index ] . transform.position );
Debug.Log(“33”); //just to get informed
}
}

public float followRange=10.0f;
public float GetDistance()
{
return (transform.position - Target.transform.position).magnitude;
}

void Update() // it also can have something to do with this, but the main suspect is Follow();
{
Target = player.transform;

if (GetDistance() < followRange)
{
Follow();
Debug.Log(“999999”); //just to get informed
}
else
{
Patrol();
Debug.Log(“8888888”); //just to get informed
}
}

You shouldn’t have to recalculate your navmesh unless the scene geometry changes. When AIs change from one state to another there’s no need to do it. You can optomize by just taking out the navmesh recalculation, and just do that on scene load.

I don’t even know if it is recalculating the navmesh…?
as of now this is waht the on start; looks like:

void Start()
{
NavMesh.pathfindingIterationsPerFrame = 10;
waypoints = GameObject.FindGameObjectsWithTag(“Waypoints”);
agent = GetComponent();
Patrol();
}

Learn to measure performance by millisecs to know what is causing the problem. FPS will never ever tell you.

It is the initiation of agent.SetDestination ( Target.transform.position ); on the update().
I mean it’s been a long week. day n night. (-) that close to chucking a window through my laptop… :smile:
And so far to my research, I’ve come to terms that anything with a chasing function, has a state transfer issue…
It’s been a long week of wall\table\ect. head banging.
And as I debug it seems to repeat the follow command roughly 100 x per second, while target is in L.O.S of enemies
I’m thinking its because the player updates its position so a new route has to be established, but it also does the same on Vector3.movement and on transform.movement. I know I might have to write a whole new mechanism for follow, that is some how node biased and it recalculates new nodes towards the target every so and often. I’m just on a really tight deadline due to a bit of slack and misguidance from project start. Wednesday is deadline, and I’m already using day and night for this part, any guidance, reference, articles,