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 MarcaisDesign · Apr 17, 2017 at 07:26 PM · fpsnavmeshnavmeshagentnavigationnav mesh

Problem with navagent killing FPS

Hey everyone, been working on a zombie survival game, I know I know, a dime a dozen now, but I honestly have been working on it for about 6 months or so. So cant say its just one of the run of the mill copy and paste projects in the works, but my problem I am facing is, I have almost my whole game finally ready for a soft alpha test coming out. But I am having some big problems with getting the nav agent to function smoothly with out really lowering my fps, I can have about 30-40 zombies on a single map with no problem but I am looking for much more. I have finally narrowed down the script that is causing this, and was really hoping someone could give me some pointers on how to improve my nav script. I have read that you can make the update time of the nav agent like load every 2 seconds or 3 instead of every second but I am not sure where it is or how to go about doing this, any help would be so much appreciated! Thank you so much and this community is so wonderful!

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 [RequireComponent (typeof(CharacterSystem))]
 public class AICharacterControllerNav : NetworkBehaviour
 {
     public string[] TargetTag = { "Player" };
     public GameObject ObjectTarget;
     [HideInInspector]
     public Vector3 PositionTarget;
     [HideInInspector]
     public CharacterSystem character;
     [HideInInspector]
     public float DistanceAttack = 2;
     public float DistanceMoveTo = 20;
     public float TurnSpeed = 10.0f;
     public bool BrutalMode;
     public bool RushMode;
     public float PatrolRange = 10;
     [HideInInspector]
     public Vector3 positionTemp;
     [HideInInspector]
     public int aiTime = 0;
     [HideInInspector]
     public int aiState = 0;
     private float attackTemp = 0;
     public float AttackDelay = 0.5f;
     public float IdleSoundDelay = 10;
     private float jumpTemp, jumpTime, soundTime, soundTimeDuration;
     public int JumpRate = 20;
     private AIManager AImange;
     public NavAgent NavObject;
     private NavAgent nav;
 
     void Start ()
     {
         character = this.gameObject.GetComponent<CharacterSystem> ();
         positionTemp = this.transform.position;
         aiState = 0;
         attackTemp = Time.time;
         jumpTemp = Time.time;
         soundTime = Time.time;
         soundTimeDuration = Random.Range (0, IdleSoundDelay);
         character.ID = "";
         if (NavObject) {
             GameObject navobj = (GameObject)GameObject.Instantiate (NavObject.gameObject, this.transform.position, this.transform.rotation);
             nav = navobj.GetComponent<NavAgent> ();
             nav.Owner = this.gameObject;
 
         }
     }
 
     public void Attack (Vector3 targetDirectiom)
     {
         if (Time.time > attackTemp + AttackDelay) {
             Vector3[] dirs = new Vector3[1];
             dirs [0] = targetDirectiom.normalized;
             character.DoDamage (this.transform.position + character.DamageOffset, dirs, character.Damage, character.DamageLength, character.Penetrate, "", character.Team);
             character.AttackAnimation ();
             attackTemp = Time.time;    
         }
     }
 
     public void AIDoAttack ()
     {
         if (Time.time > attackTemp + AttackDelay) {
             Vector3[] dirs = new Vector3[1];
             dirs [0] = targetDirectiom.normalized;
             character.DoDamage (this.transform.position + character.DamageOffset, dirs, character.Damage, character.DamageLength, character.Penetrate, "", character.Team);
             character.AttackAnimation ();
             attackTemp = Time.time;    
         }
     }
 
     void FrontObstacleChecker ()
     {
         // jump when hit something in front
         Vector3 fwd = this.transform.TransformDirection (Vector3.forward);
         if (Physics.Raycast (this.transform.position, fwd, 1)) {
             if (Time.time >= jumpTemp + jumpTime) {
                 character.Motor.inputJump = true;
                 jumpTime = Random.Range (0, JumpRate) * 0.1f;
                 jumpTemp = Time.time;
             } else {
                 character.Motor.inputJump = false;        
             }
         }
         
     }
 
     Vector3 targetDirectiom;
 
     void Update ()
     {
         if (character == null)
             return;
         
         // random play an idle sound.
         if (Time.time > soundTime + soundTimeDuration) {
             character.PlayIdleSound ();    
             soundTimeDuration = Random.Range (0, IdleSoundDelay);
             soundTime = Time.time;
         }
 
         if (isServer && character.IsAlive) {
             character.Motor.IsServerControl = true;
             // get attack distance from primary weapon.
             DistanceAttack = character.PrimaryWeaponDistance;    
 
             float distance = Vector3.Distance (PositionTarget, this.gameObject.transform.position);
             targetDirectiom = (PositionTarget - this.transform.position);
 
             // rotate facing to nav agent
             if (nav) {
                 //targetDirectiom = (nav.transform.position - this.transform.position);
             }
 
             Quaternion targetRotation = this.transform.rotation;
             float str = TurnSpeed * Time.time;
             // rotation to look at a target
             if (targetDirectiom != Vector3.zero) {
                 targetRotation = Quaternion.LookRotation (targetDirectiom);
                 targetRotation.x = 0;
                 targetRotation.z = 0;
                 transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
             }
             
             FrontObstacleChecker ();
             
             // if Target is exist
             if (ObjectTarget != null) {
                 PositionTarget = ObjectTarget.transform.position;
                 if (aiTime <= 0) {
                     aiState = Random.Range (0, 4);
                     aiTime = Random.Range (10, 100);
                 } else {
                     aiTime--;
                 }
                 
                 // attack only distance.
                 if (distance <= DistanceAttack) {
                     if (aiState == 0 || BrutalMode) {
                         Attack (targetDirectiom);
                     }
                 } else {
                     if (distance <= DistanceMoveTo) {
                         // rotation facing to a target.
                         transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
                     } else {
                         // if target is out of distance
                         ObjectTarget = null;
                         if (aiState == 0) {
                             aiState = 1;
                             aiTime = Random.Range (10, 500);
                             PositionTarget = positionTemp + new Vector3 (Random.Range (-PatrolRange, PatrolRange), 0, Random.Range (-PatrolRange, PatrolRange));
                         }
                     }
                 }
 
             } else {
     
                 float length = float.MaxValue;
 
                 for (int t = 0; t < TargetTag.Length; t++) {
                     // Finding all the targets by Tags.
                     GameObject[] targets = (GameObject[])GameObject.FindGameObjectsWithTag (TargetTag [t]);
                     if (targets != null && targets.Length > 0) {
                         for (int i = 0; i < targets.Length; i++) {
                             DamageManager targetdamagemanager = targets [i].GetComponent<DamageManager> ();
                             if (targetdamagemanager != null && targetdamagemanager.IsAlive) {
                                 float distancetargets = Vector3.Distance (targets [i].gameObject.transform.position, this.gameObject.transform.position);
                                 if ((distancetargets <= length && (distancetargets <= DistanceMoveTo || distancetargets <= DistanceAttack || RushMode)) && ObjectTarget != targets [i].gameObject) {
                                     length = distancetargets;
                                     ObjectTarget = targets [i].gameObject;
                                 }
                             }
                         }
                     }
                 }
                 if (aiState == 0) {
                     // AI state == 0 mean AI is free, so moving to anywhere
                     aiState = 1;
                     aiTime = Random.Range (10, 200);
                     PositionTarget = positionTemp + new Vector3 (Random.Range (-PatrolRange, PatrolRange), 0, Random.Range (-PatrolRange, PatrolRange));
                 }
                 if (aiTime <= 0) {
                     // random AI state
                     aiState = Random.Range (0, 4);
                     aiTime = Random.Range (10, 200);
                 } else {
                     aiTime--;
                 }
             
             
             }
 
             Vector3 positiongo = PositionTarget;
 
             // move following nav agent
             if (nav) {
                 nav.SetTarget (PositionTarget);
                 nav.navMeshAgent.speed = character.MoveSpeedMax;
 
                 if ((nav.navMeshAgent.pathStatus != NavMeshPathStatus.PathPartial && nav.navMeshAgent.hasPath) || ObjectTarget == null) {
                     positiongo = nav.transform.position;
                 }
             }
 
             character.MoveToPosition (positiongo);
         }
     }
 }
 
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

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

Problem with Unity NavMesh tools 1 Answer

NavMesh working but not allowing me to deal damage to the enemy 0 Answers

Navigation 0 Answers

for nav mesh agent, why is it that the base offset is 1 instead of 0 as default and why is setting it to 0 make the cylinder colider move down? 0 Answers

Problem with NavMesh 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