• 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 Magic-Max · Mar 19, 2017 at 08:36 AM · c#artificial intelligencepatrol

How do I get the AI to patrol back to his points once I am not in his vision anymore?

I am trying to create a script where the AI patrols and when you are in his field of vision he will start chasing you. As soon as you are not in his field of vision anymore he is supposed to go back to his patrol point and patrol again. So far, what the AI does is, he chases me and as soon as he stops chasing me he either goes back to one of the patrol points and stands there or moves with low speed. How do I fix this I can't seem to find a solution. Could anyone please help me out? :)

Here is the script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

public class PatrolScript : MonoBehaviour {

 public Transform[] path;
 public float patrolSpeed = 5f;
 public float reachDistance = 1f;   
 public int currentPoint = 0;
 public GameObject target;
 public float aiVision = 5f;
 private NavMeshAgent myAgent;
 Transform me;

 void Start()
 {
     myAgent = GetComponent<NavMeshAgent>();
 }
 void Update()
 { 
     if (Vector3.Distance(transform.position, target.transform.position) < aiVision)
     {
         myAgent.SetDestination(target.transform.position);
     }
     else
     {
         {
             float distance = Vector3.Distance(path[currentPoint].position, transform.position);
             transform.position = Vector3.MoveTowards(transform.position, path[currentPoint].position, Time.deltaTime * patrolSpeed);

             if (distance <= reachDistance)
             {
                 currentPoint++;
             }

             if (currentPoint >= path.Length)
             {
                 currentPoint = 0;
             }
         }
     }
 }
 void OnDrawGizmos()
 {
     if (path.Length > 0)
         for (int i = 0; i < path.Length; i++)
         {
             if (path[i] != null)
             {
                 Gizmos.DrawSphere(path[i].position, reachDistance);
             }
         }
 }


}

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Initialising List array for use in a custom Editor 1 Answer

How can i make my soldier to patrol over the space station in random ? 1 Answer

Unity Navmesh agent patrol and chase player script issues. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges