PathFinding Lag With NaMeshAgent
Hi i'm trying to make a tower defence game. The AI uses unity's navmesh agent for path finding.
Atm the AI has a Sphere collider that adds and removes enemies in sight range of the AI. The AI can do one of three things. Head to the players base if no targets in range. Attack a target or a player or look for a player if the player runs out of there line of sight.
The AI behavior works as intended however with only 4 AI in the scene i'm getting 20fps, regardless of what state they are in. The AI are 7k tri's each and all are animated. The scene is just a bunch of cubes.The game Potentially could have 100 of these AI in scene at once so i need to optimize it.
I'm assuming it has something to do with how i'm calling/ have often i'm calling upon the navMeshAgent What have i done wrong?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class AICharacter : MonoBehaviour {
NavMeshAgent agent;
CharacterMovement charMove;
float targetTolerance = 1;
Animator anim;
WeaponManager weaponManager;
float patrolTimer;
public float waitingTime = 5;
Vector3 targetPos;
public List<GameObject> Enemies = new List<GameObject>();
public GameObject enemyToAttack;
private float attackRate = 0.8f ;
private float attackTimer;
private EnemyStats enemyStats;
private bool inSight;
public float aggroRange = 20;
public AIState aiState;
public enum AIState
{
Patrol,
Attack,
FindPlayer
}
// Use this for initialization
void Start () {
agent = GetComponentInChildren<NavMeshAgent>();
charMove = GetComponent<CharacterMovement>();
anim = GetComponent<Animator>();
weaponManager = GetComponent<WeaponManager>();
enemyStats = GetComponent<EnemyStats>();
}
void DecideState()
{
if (Enemies.Count > 0)
{
if (!enemyToAttack)
{
float distance = Mathf.Infinity;
foreach (GameObject engo in Enemies)
{
Vector3 diff = engo.transform.position - transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
enemyToAttack = engo;
distance = curDistance;
}
}
}else if(Vector3.Distance(transform.position, enemyToAttack.transform.position) > aggroRange){
Debug.Log("Enemy is null");
enemyToAttack = null;
}
if (enemyToAttack)
{
CheckLineOfSight();
}
else if (inSight)
{
aiState = AIState.Attack;
}
else
{
aiState = AIState.FindPlayer;
}
}
else
{
if (enemyToAttack != null)
{
enemyToAttack = null;
}
Debug.Log("Set State patrol");
aiState = AIState.Patrol;
}
}
/* void OnAnimatorIK(){
if (enemyToAttack)
{
anim.SetLookAtWeight(1, 0.8f, 1, 1, 1);
anim.SetLookAtPosition(enemyToAttack.transform.position);
}
else
{
anim.SetLookAtWeight(0);
}
}*/
void Attack(){
Debug.Log("Attacking");
agent.Stop();
anim.SetFloat("Turn", 0);
anim.SetFloat("Forward", 0);
weaponManager.aim = true;
charMove.Move(Vector3.zero, true, enemyToAttack.transform.Find("Hitpos").transform.position);
Vector3 direction = enemyToAttack.transform.position - transform.position;
float angle = Vector3.Angle(direction, transform.forward);
attackTimer += Time.deltaTime;
if (attackTimer > attackRate)
{
ShootRay();
attackTimer = 0;
}
}
public void CheckLineOfSight(){
RaycastHit hit;
Vector3 directionToAttack;
Vector3 startPos = weaponManager.activeWeapon.bulletSpawnPoint.TransformPoint(Vector3.zero);
directionToAttack = enemyToAttack.transform.position - transform.position;
Debug.DrawRay(startPos, directionToAttack, Color.red);
if (Physics.Raycast(startPos, directionToAttack, out hit, Mathf.Infinity, mask))
{
if (hit.transform.root == enemyToAttack.transform.root)
{
inSight = true;
}
else if (hit.transform.root.tag == "Player" || hit.transform.root.tag == "Tower")
{
inSight = true;
}
else
{
inSight = false;
}
}
}
public LayerMask mask;
void ShootRay(){
anim.SetTrigger("Fire");
RaycastHit hit;
GameObject go = Instantiate(weaponManager.activeWeapon.bulletPrefab, weaponManager.activeWeapon.bulletSpawnPoint.position, Quaternion.identity) as GameObject;
LineRenderer line = go.GetComponent<LineRenderer>();
Vector3 startPos = weaponManager.activeWeapon.bulletSpawnPoint.TransformPoint(Vector3.zero);
Vector3 endPos = Vector3.zero;
Vector3 directionToAttack;
directionToAttack = enemyToAttack.transform.Find("Hitpos").transform.position - weaponManager.activeWeapon.bulletSpawnPoint.transform.position;
if (Physics.Raycast(startPos, directionToAttack, out hit, Mathf.Infinity, mask))
{
if (hit.transform.GetComponent<Rigidbody>() && hit.transform.tag != "Player")
{
Vector3 direction = hit.transform.position - transform.position;
direction = direction.normalized;
// hit2.transform.GetComponent<Rigidbody>().AddForce(direction * 200);
}
else if(hit.transform.GetComponent<CharacterStats>())
{
hit.transform.GetComponent<CharacterStats>().TakeDamage((int)enemyStats.damage);
hit.transform.GetComponent<CharacterAudioManager>().PlayEffect("Hit");
}else if(hit.transform.GetComponentInChildren<BaseTower>()){
Debug.Log("Hit Tower");
hit.transform.root.GetComponentInChildren<BaseTower>().TakeDamage((int)enemyStats.damage);
}
if (weaponManager.activeWeapon.bulletHitParticle != null)
Instantiate(weaponManager.activeWeapon.bulletHitParticle, hit.point,Quaternion.identity );
endPos = hit.point;
}
line.SetPosition(0, startPos);
line.SetPosition(1, endPos );
if (weaponManager.activeWeapon.transform.GetComponent<AudioSource>())
weaponManager.activeWeapon.GetComponent<AudioSource>().Play();
}
public GameObject playerBase;
void Patrolling(Vector3 target)
{
Debug.Log("Patroling");
agent.speed = 1;
agent.Resume();
MoveTo(target, false, playerBase.transform.position);
}
// Update is called once per frame
void Update () {
if (enemyStats.alive)
{
DecideState();
switch (aiState)
{
case AIState.Attack:
Attack();
break;
case AIState.Patrol:
Debug.Log("PatrolState");
Patrolling(playerBase.transform.position);
break;
case AIState.FindPlayer:
FindTarget();
break;
}
}
else
{
}
}
void FindTarget(){
agent.Resume();
weaponManager.aim = true;
MoveTo(enemyToAttack.transform.position, false, enemyToAttack.transform.Find("Hitpos").transform.position);
}
void MoveTo(Vector3 destination, bool aim, Vector3 lookPos)
{
agent.transform.position = transform.position;
agent.destination = destination;
Vector3 velocity = agent.desiredVelocity * 0.5f;
Debug.Log(velocity);
charMove.Move(velocity, aim, lookPos);
}
}
Your answer
Follow this Question
Related Questions
Need help with Nav Mesh Agent getting "stuck" at high speeds 0 Answers
Navmesh has some problems? 0 Answers
How to assure Navagents maintain a certain distance from one another while following same path? 0 Answers
How to exclude certain navmesh obstacles on runtime? 2 Answers
Use NavMesh to traverse a random path 0 Answers