Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Schizofrenzy · Mar 09, 2014 at 06:46 AM · pathfind

What is the best way to do a patrolling AI?

I want to set up an enemy AI such that they patrol some area and upon registering the player in front of them, switch to some other action. I know there is some sort of built in pathfinding stuff but I don't know how to set up patrol points with it.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by JNetRocks1316 · Mar 09, 2014 at 07:20 AM

Short Version:

  • Create an enemy game object

  • Attach a Nav Mesh Agent

  • Create your nav mesh (Navigation window)

  • Create conditions for the enemy to follow

Example of a simple "wandering" enemy.

 using UnityEngine;
 using System.Collections;
 
 public class PatrollingEnemy : MonoBehaviour {
 
     private Vector3 startPosition;  //Give it a startPosition so it knows where it's 'home' location is.
     private bool wandering = true;  //Set a bool or state so it knows if it's wandering or chasing a player
     private bool chasing = false;
     private float wanderSpeed = 0.5f;  //Give it the movement speeds
     private GameObject target;  //The target you want it to chase
 
 
     //When the enemy is spawned via script or if it's pre-placed in the world we want it to first
     //Get it's location and store it so it knows where it's 'home' is
     //We also want ti set it's speed and then start wandering
     void Awake(){
         //Get the NavMeshAgent so we can send it directions and set start position to the initial location
         agent = GetComponent("NavMeshAgent") as NavMeshAgent;
         agent.speed = wanderSpeed;  
         startPosition = this.transform.position;
         //Start Wandering
         InvokeRepeating("Wander", 1f, 5f);
     }
 
     //When we wander we essentially want to pick a random point and then send the agent there
     //Random.Range is perfect for this.
     //If you're working on a hilly terrain you may want to change your y to a higher point and then
     //Use a raycast down to hit the 'terrain' point, rather than keeping y at 0.
     //y at 0 would only work if you have a completely flat floor.
     void Wander(){
         //Pick a random location within wander-range of the start position and send the agent there
         Vector3 destination = startPosition + new Vector3(Random.Range (-wanderRange, wanderRange), 
                                                           0, 
                                                           Random.Range (-wanderRange, wanderRange));
         NewDestination(destination);
     }
 
 
     //Creating this as it's own method so we can send it directions other when it's just wandering.
     public void NewDestination(Vector3 targetPoint){
         //Sets the agents new target destination to the position passed in
         agent.SetDestination (targetPoint);
     }
 }


As far as detecting the player you could do that through a trigger or a ray-cast to see if they are in visual range. For example, you could have an Update loop that does a raycast out from the enemy's face and if the enemy 'sees' the player (the ray hits the player), then trigger another method that changes the 'state' from wandering to 'chasing' or 'running' or whatever behavior you want it to do!

I'm pretty new to programming so there might be a more efficient way, but it does work!

Comment
Add comment · Show 1 · 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 Musty · Jun 22, 2014 at 12:24 PM 0
Share

If InvokeRepeating("Wander", 1f, 5f); is running in awake and you set it to chase player then how do you set it back to wandering once it chased the player for too long?

avatar image
1

Answer by sundhar_unity · Oct 27, 2020 at 04:52 AM

https://youtu.be/BIyYldTyyR8

This video might Help on Patrol

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
avatar image
0

Answer by fafase · Mar 09, 2014 at 08:11 AM

http://unitygems.com/basic-ai-character/

This may help since it shoes how to get an AI to patrol or attack when the player is close enough, within line of sight and not hidden behind a wall.

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

21 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

Related Questions

waypoint and navmesh 1 Answer

Argument Out of Range On a List 2 Answers

NullRefException in RaycastAll 2 Answers

Is it possible with Unity's pathfinding? 1 Answer

Waypoint pathfinding gone terribly wrong. 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