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 /
  • Help Room /
avatar image
0
Question by dzuritaa · Sep 02, 2020 at 04:57 AM · 3dai

Simple AI for Unity to patroll and follow the player

Well hello there I´m making a small game where there is a demon that has to follow a patrol area and when it sees the player it has to chase it. After watching some tutorials I have created the code for the demon to patrol and to follow the player. But I haven´t been able to make the demon do the 2 things. So my code looks like this.

 public class Gozu : MonoBehaviour
 {
     //Dictates whether the agent waits on each node
     [SerializeField] private bool patrolWaiting;
     
     //The total time we wait at each node
     [SerializeField] private float totalWaitTime = 3f;
     
     //The progagility of switching direction.
     [SerializeField] private float switchProbability = 0.2f;
     
     //The list of all patrol nodes to visit
     [SerializeField] private List<Waypoint> patrolPoints;
 
     //private variables for base behaviour
     private NavMeshAgent navMesh;
     private int currentPatrolIndex;
     private bool travelling;
     private bool waiting;
     private bool patrolFoward;
     private float waitTimer;
     
     public Transform player;
     private Animator anim;
     private string state = "IDLE";
     private Vector3 direction;
     private float angle;
     
     private float rotationSpeed = 2.0f;
     private float speed = 2.0f;
     private float visDist = 20.0f;
     private float visAngle = 30.0f;
     private float attackDist = 1.0f;
 
 
     // Start is called before the first frame update
     void Start()
     {
         //get the animations to trigger them
         anim = this.GetComponent<Animator>();
         //gets the NavMesh for the patrol
         navMesh = this.GetComponent<NavMeshAgent>();
         //starts the count of the patrol points and makes the demon start on a point
         if (patrolPoints != null && patrolPoints.Count >=2)
         {
             currentPatrolIndex = 0;
             SetDestination();
         }
     }
 
     
     // Update is called once per frame
     void Update()
     {
         //gets the updated position of the player
         direction = player.position - this.transform.position;
         //get the angle foward of demon
         angle = Vector3.Angle(direction, this.transform.forward);
         //evaluates if the player is in the cone of vision of the demon
         if (direction.magnitude < visDist && angle < visAngle)
         {
             FollowPlayerCone(direction);
         }
         else
         {
             PatrolGozu();
         }
         
         //SeekPlayer(player.transform.position);
     }
 
     //sets the destinatios of patrol points
     private void SetDestination()
     {
         if (patrolPoints != null)
         {
             Vector3 targetVector = patrolPoints[currentPatrolIndex].transform.position;
             navMesh.SetDestination(targetVector);
             travelling = true;
         }
     }
 
     //changes the patrol point
     private void ChangePatrolPoint()
     {
         if (UnityEngine.Random.Range(0f, 1f) <= switchProbability)
         {
             patrolFoward = !patrolFoward;
         }
 
         if (patrolFoward)
         {
             currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Count;
         }
         else
         {
             if (--currentPatrolIndex < 0)
             {
                 currentPatrolIndex = patrolPoints.Count - 1;
             }
         }
     }
 
     //follows the player f is in cone/angle of vision 
     private void FollowPlayerCone(Vector3 direction)
     {
         direction.y = 0;
         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
         
         if (direction.magnitude > attackDist)
         {
             if (state != "RUNNING")
             {
                 state = "RUNNING";
                 anim.SetTrigger("isRunning");
             }
         }
         else
         {
             if (state != "ATTACKING")
             {
                 state = "ATTACKING";
                 anim.SetTrigger("isAttacking");
             }
         }
         
         //follow 
         if (direction.magnitude < visDist && angle < visAngle)
         {
 
         }
         else
         {
             if (state != "IDLE")
             {
                 state = "IDLE";
                 anim.SetTrigger("isIdle");
             }
         }
 
         if (state == "RUNNING")
         {
             this.transform.Translate(0,0, speed * Time.deltaTime);
         }
     }
     
     //patrol demon
     private void PatrolGozu()
     {
         //Check if we're close to the destination
         if (travelling && navMesh.remainingDistance <= 1.0f)
         {
             travelling = false;
             
             //fi we're going to wait, then wait
             if (patrolWaiting)
             {
                 waiting = true;
                 waitTimer = 0f;
             }
             else
             {
                 ChangePatrolPoint();
                 SetDestination();
             }
         }
 
         if (waiting)
         {
             waitTimer += Time.deltaTime;
             if (waitTimer >= totalWaitTime)
             {
                 waiting = false;
 
                 ChangePatrolPoint();
                 SetDestination();
             }
         }
     }
 }

I know that the cone of vision of the demon it's working, but when patroling it does not stop to follow the player and continues to patrol between the points.

I'm sure I'm missing something, and since I'm relatively new to Unity right now I'm clueless.

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.

Follow this Question

Answers Answers and Comments

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

How to keep vehicles in correct lanes 0 Answers

Where to start learning about AI? 1 Answer

Help to make enemy AI not go through terrain and not fly when chasing player up hills. 0 Answers

Problems with basic enemyAI on sphereical world 0 Answers

Help with enemy AI 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