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 /
avatar image
-1
Question by Zitoox · Nov 03, 2016 at 07:33 PM · c#navmeshagentconfiguration

NavMesh Agent question

I have been reading about Navmesh Agent and i decided that this is the best place to start making the Enemy AI of my FPS. Navmesh allow an object to follow another object, avoiding stactic objects, turning smoothly towards it's target, turning around when blocked, etc...

It's basically a FOLLOW AI. But i wanted to know if i can customize it to also do the following:

If the target is behind a house for example, then the enemy will NOT follow him, but instead patrol in the house's area surroudings. And if the enemy is ........... meters away from the player, then stop and follow him again if the player is not .......... meters from the enemy.

A friend helped me making a script which you attach to a gameobject with a navmesh agent, and then you specify the gameobject's target. If the answer for this is by using script, here it is:

 using UnityEngine;
 using System.Collections;
 
 public class NavFollow : MonoBehaviour {
 
     
     public Transform target; //Usually the player
 
     
     void Start () {
         StartCoroutine(FollowEnemyCoordinates());
     }
     
     
     void Update () {
         
     }
 
     //This will set the target position for the NavmeshAgent every so seconds so we don't waste performance
     IEnumerator FollowEnemyCoordinates() {
 
         while (true)
         {
             //This will set the target coordinates
             GetComponent<NavMeshAgent>().SetDestination(target.position);
             //This will wait for 1 second before looping
             yield return new WaitForSeconds(1);
         }
     }
 }

Could someone experienced in NavMesh or at least someone that know how to make this help me? This would help a lot.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cynikal · Nov 03, 2016 at 11:22 PM

So, Let's break this down.

  1. If the player is behind an object, do not follow

  2. If the enemy and player are so far away, stop following.

For #1: Use a Raycast always pointing to the player from the enemy. -- If the Raycast hits the player directly, he has light of sight. Follow. If it hits anything other than the player, line of sight is lost. Stop following.

For #2: Use Vector3.Distance(Player Position, Enemy Position).

 if (Vector3.Distance(Player.Position, Enemy.Position) >= MaxFollowDistance) StopFollowing();

To stop an agent, simple use .Stop(); on the nav mesh agent.

Further more, for optimization:

You shouldn't keep referencing your NavMeshAgent via GetComponent. It's a "heavy" feature.

What you should do is:

 public class of whatever {
 NavMeshAgent nma;
 
 void Start()
 {
 nma = GetComponent<NavMeshAgent>();
 }
 }

Then instead of hitting your

 //Instead of: GetComponent<NavMeshAgent>().SetDestination(target.position);
 //Use:
 nma.SetDestination(target.position);
Comment
Add comment · 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

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

58 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Subtracting the position of transform from the position of Game Objects in a list. 1 Answer

Character rotation (click-to-move) 0 Answers

Navmeshagent wont go after player after stopping! 2 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