Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Chippers · Nov 23, 2016 at 07:50 AM · 3dnavmeshagentnavigationstate-machineartificial intelligence

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);
     }
 
   
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges