Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by vithorescames · Oct 17, 2016 at 05:01 AM · playeraienemynavmeshagentchase

Enemy's NavMesh not working

Hey guys.

I'm new in Unity and I don't know why my code doesn't work. When range is under 15, enemy should chase player, but instead of this it stops. Also, it is a little lagged when arrives at each point. How can I resolve this?

 public Transform[] patrolPoints;
 public float moveSpeed;
 private int currentPoint;
 private GameObject Player1;
 private  GameObject currentPlayer;
 private bool Patrolling = true;
 private NavMeshAgent navComp;

 // Use this for initialization
 void Start () {        

     currentPoint = 0;
     Player1 = GameObject.FindGameObjectWithTag ("Player");
     currentPlayer = Player1;
     navComp = this.GetComponent<NavMeshAgent> ();
 //    transform.position = patrolPoints [0].position;
 }

 // Update is called once per frame
 void Update () {
     
     float range = Vector3.Distance (transform.position, currentPlayer.transform.position);
     transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);

     if (Patrolling) {

         if (transform.position == patrolPoints [currentPoint].position) {
             currentPoint++;
         }

         if (currentPoint >= patrolPoints.Length) {
             currentPoint = 0;
         }

         if (range <= 15) {
             Patrolling = false;
         }
         //transform.LookAt (patrolPoints [currentPoint].transform.position);        

     }

     else if (Patrolling == false) {

         //transform.position = Vector3.MoveTowards (transform.position, currentPlayer.transform.position, moveSpeed * Time.deltaTime);
         navComp.SetDestination(currentPlayer.transform.position);

         if (range >= 20) {
             Patrolling = true;
         }
     
         }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vithorescames · Oct 17, 2016 at 12:10 PM

I figured out what was happening. Movetowards shouldn't be free in Update! Here is the code, working now!

 public Transform[] patrolPoints;
 public float moveSpeed;
 private int currentPoint;
 private GameObject Player;
 private bool Patrolling = true;
 private NavMeshAgent navComp;

 // Use this for initialization
 void Start () {        

     currentPoint = 0;
     Player = GameObject.FindGameObjectWithTag ("Player");    
     navComp = this.GetComponent<NavMeshAgent> ();
 //    transform.position = patrolPoints [0].position;
 }

 // Update is called once per frame
 void Update () {
     
     float range = Vector3.Distance (transform.position, Player.transform.position);

     if (range <= 20) {
         Patrolling = false;
     }
     else if (range >= 20) {
         Patrolling = true;
     }

     if (Patrolling) {

         navComp.enabled = false;

         transform.LookAt (patrolPoints [currentPoint].transform.position);
         transform.position = Vector3.MoveTowards (transform.position, patrolPoints [currentPoint].position, moveSpeed * Time.deltaTime);

         if (transform.position == patrolPoints [currentPoint].position) {
             currentPoint++;
         }

         if (currentPoint >= patrolPoints.Length) {
             currentPoint = 0;
         }

     }

     else if (Patrolling == false) {

         //transform.position = Vector3.MoveTowards (transform.position, currentPlayer.transform.position, moveSpeed * Time.deltaTime);
         navComp.enabled = true;
         navComp.SetDestination(Player.transform.position);

     
         }

     }
 
 }
Comment
Add comment · Show 2 · Share
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
avatar image ZBlaxland · Oct 17, 2016 at 12:12 PM 0
Share

Awesome!!! post that as the answer so that people know :D

avatar image FreshDeveloper · Aug 08, 2020 at 11:12 AM 0
Share

Thank you so much, you saved me after 9 hours of debugging! I really owe you

avatar image
0

Answer by ZBlaxland · Oct 17, 2016 at 07:24 AM

I don't know if it helps, but here is a JavaScript that you may be able to use instead:

 var theplayer : GameObject;
 var speed : float;
 var range : int;
 var explosion : GameObject;
 function Update () {
 
 //RANGE - calculate the distance between the enemy and the player
 range=Vector3.Distance(theplayer.transform.position,transform.position);
 
 //AWARENESS – make the enemy look at the player when within 40 meters
 if (range<40){
     transform.LookAt(theplayer.transform.position);
 }
 
 //CHASE - move toward the player when within 15 and 30 meters
 if (range<30&&range>15){
     transform.Translate(Vector3.forward*speed);
 }
     
 //RETREAT – move away from the player when within 12 meters
 if (range<12){
     transform.Translate(-Vector3.forward*speed);
 }
 
 //DESTROY – Create an explosion and destroy the enemy when within 5 meters
 if (range<5){
     Instantiate(explosion,transform.position,transform.rotation);
     Destroy(gameObject);
 }
 }
 

The values for speed and whatnot are easily changed, just remember to drag you character etc. into the given spaces on the script in the inspector before loading the game

Hope it works for you :D

Comment
Add comment · Show 2 · Share
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
avatar image vithorescames · Oct 17, 2016 at 11:28 AM 0
Share

Oh, I used a code similar to this, but I need the Nav$$anonymous$$esh to patchfinding, you know? I apreccitate your effort!

avatar image ZBlaxland vithorescames · Oct 17, 2016 at 11:49 AM 0
Share

Sorry I have no idea then :/

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NavMeshAgent does not follow player 0 Answers

NavMesh problem it does not respond corectly 0 Answers

Getting the player to retreat a certain distance from the player 1 Answer

NavMesh flee. Ai flee from player. 4 Answers

Problem using boids algorithm for chasing enemies. 1 Answer


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