• 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 KenanTheFab · Jun 29 at 11:53 PM · ainavmeshnavmeshagentartificial intelligencebehaviour

AI won't change state between hunting and wandering.

Hello, I am having an issue with my AI's states.

My AI refuses to return to a wandering state after entering the hunting state- even when the path to the player is cut off/obstructed.

When the door is closed at the beginning the AI stays in a wandering state as it isn't able to make its way to the player, which works great... but opening the door to let the AI enter hunting mode will keep it in hunting mode no matter how long the door stays closed or if the path is cut at all AI outside the door. The big magenta beam is for debugging. It constantly sets itself to the goal of the AI. It appears to get stuck outside the door even when the AI is let inside of the room- but will then snap to the AI once it is trapped in said room.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 public class MannequinAI : MonoBehaviour
 {
     public bool activated;
     public NavMeshAgent agent;
     public GameObject victim;
     public Camera cam;
     public float teleportSpeed;
     private float currentSpeed;
     private float distance;
     public float maxDistance;
     private bool watching;
     public GameObject goalPost;
     bool wandering;
     [Header("1: hunting\n2: wandering\n3: idle")]
     public int aiState;
     NavMeshPath navMeshPath;

 private void Start()
 {
     agent = GetComponent<NavMeshAgent>();
     navMeshPath = new NavMeshPath();
     agent.CalculatePath(victim.transform.position, navMeshPath);
     currentSpeed = teleportSpeed;
 }

 private void Update()
 {
     if (activated)
     {
         switch (navMeshPath.status)
         {
             case NavMeshPathStatus.PathComplete:
                 print("a honter must hont");
                 StopCoroutine(Wander());
                 wandering = false;
                 aiState = 1;
                 agent.SetPath(navMeshPath);
                 Hunt();
                 break;
             case NavMeshPathStatus.PathPartial:
                 if (aiState != 2)
                 {
                     aiState = 2;
                     StartCoroutine(Wander());
                     print("Wandering");
                 }
                 break;
             case NavMeshPathStatus.PathInvalid:
                 break;
         }
         goalPost.transform.position = agent.pathEndPosition;
     }
 }

 IEnumerator Wander()
 {
     wandering = true;
     while (wandering)
     {
         agent.CalculatePath(victim.transform.position, navMeshPath);
         Vector3 randomDir = (UnityEngine.Random.insideUnitSphere * 10) + transform.position;
         NavMesh.SamplePosition(randomDir, out NavMeshHit hit, 10, 1);
         Vector3 wanderGoal = hit.position;
         agent.SetDestination(wanderGoal);
         yield return new WaitForSeconds(2);
     }
 }

 void Hunt()
 {
     print("hunting");
     //calculate distance between enemy and player
     distance = Vector3.Distance(victim.transform.position, transform.position) / maxDistance;
     //calculate the speed at which the enemy moves based on how far away the player is.
     currentSpeed = Mathf.Lerp(0, teleportSpeed, distance);
     agent.acceleration = currentSpeed;
     agent.speed = currentSpeed;

     //find player and store where to rotate.
     Vector3 direction = (victim.transform.position - transform.position).normalized;
     //turn towards player.
     Quaternion lookAtThis = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 100 * Time.deltaTime);
     transform.rotation = lookAtThis;


     //chase player
     Vector3 victimPos = victim.transform.position;
     agent.isStopped = false;
     agent.SetDestination(victimPos);
     if (watching)
     {
         //prevent it from looking up or down and only turn left and right on a swivel
         transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
         //stops AI in its tracks when viewed.
         agent.isStopped = true;
         agent.velocity = Vector3.zero;
         currentSpeed = 0;
     }
 }


If you have any questions feel free to ask.

Comment
Add comment · Show 1
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 KenanTheFab · Jun 30 at 08:46 AM 0
Share

bumping since i posted so late

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

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

224 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

Related Questions

NavMeshAgent occasionally makes stale paths 0 Answers

Is there any way to make the vertical automatically-generated Mesh Links from the NavMesh bidirectional? 0 Answers

Checking if a non-walkable layer is blocking the path? (NavMeshAgent) 0 Answers

NavMeshAgent resume original position and facing direction C# 0 Answers

My NavMeshAgent doesn't move at all!,Why my NavMeshAgent doesn't move at all? 0 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