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
}
}