• 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 t$$anonymous$$s it stops. Also, it is a little lagged when arrives at each point. How can I resolve t$$anonymous$$s?

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

 // Use t$$anonymous$$s for initialization
 void Start () {        

     currentPoint = 0;
     Player1 = GameObject.FindGameObjectWithTag ("Player");
     currentPlayer = Player1;
     navComp = t$$anonymous$$s.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 t$$anonymous$$s for initialization
 void Start () {        

     currentPoint = 0;
     Player = GameObject.FindGameObjectWithTag ("Player");    
     navComp = t$$anonymous$$s.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
avatar image FreshDeveloper · Aug 08, 2020 at 11:12 AM 0
Share
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 wit$$anonymous$$n 40 meters
 if (range<40){
     transform.LookAt(theplayer.transform.position);
 }
 
 //CHASE - move toward the player when wit$$anonymous$$n 15 and 30 meters
 if (range<30&&range>15){
     transform.Translate(Vector3.forward*speed);
 }
     
 //RETREAT – move away from the player when wit$$anonymous$$n 12 meters
 if (range<12){
     transform.Translate(-Vector3.forward*speed);
 }
 
 //DESTROY – Create an explosion and destroy the enemy when wit$$anonymous$$n 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
avatar image ZBlaxland vithorescames · Oct 17, 2016 at 11:49 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Enemy shaking when following player 2 Answers

Enemy detection in the light(Javascript) 1 Answer

NavMeshAgent track Player when in Range? 2 Answers

How to stop the enemy from jumping when chasing the player ? 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