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 CeratoRaptor · Dec 29, 2021 at 06:10 AM · aiwanderhiding

Wandering Code Not Working When Player Hides

I made an AI for my game, and the player has the ability to hide from said AI in places like desks and closets. The AI works fine in any other situation. However, when the player hides, the AI is supposed to give up searching and start wandering once the AI gets close to where it last saw the player. Instead, it just stops in front of the last seen position and doesn't move until it sees the player again. I've tried using a Coroutine and using a custom function for the wandering, but it doesn't work. If anyone could help, that'd be appreciated greatly.

The hiding code

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

public class hide : MonoBehaviour {

 public bool hidden;
 bool canHide;

 void Update()
 {

     playerController controller = GetComponent<playerController>();

     if (controller.crouching && canHide)
     {
         hidden = true;
     }

     if (!controller.crouching || !canHide)
     {
         hidden = false;
     }

 }
 
 void OnTriggerStay(Collider Col)
 {
     if (Col.gameObject.tag == "Hiding Spot")
     {
         canHide = true;
     }
 }

 void OnTriggerExit(Collider Col)
 {
     if (Col.gameObject.tag == "Hiding Spot")
     {
         canHide = false;
         hidden = false;
     }
 }

}

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

public class robotNavigation : MonoBehaviour {

 public float vision;
 NavMeshAgent agent;

 Transform player;

 public float mapBorderX;
 public float mapBorderY;

 public Transform wanderTarget;

 float timer = 1.81f;

 public bool inAttackRange;
 bool wandering;


 Vector3 lastPos;

 void Awake()
 {
     agent = GetComponent<NavMeshAgent>();
     player = GameObject.FindWithTag("Player").transform;
 }

 void Update()
 {
     float distance = Vector3.Distance(transform.position, player.position);
     hide hidden = player.gameObject.GetComponent<hide>();

     if (distance <= vision)
     {
         //Shoots a linecast between the players position and itself.
         if (Physics.Linecast(transform.position, player.position) && !hidden.hidden)
         {
             /*If the linecast is not blocked, sets the agents destination to the players position,
             and records where the player was last seen, as well as resetting the move timer and stopping the wander loop.*/
             wandering = false;
             agent.SetDestination(player.position);
             lastPos = player.position;
             timer = 1.81f;
         }

         /*If the player is hidden, goes to the last known position and then wanders if it can't find the player.*/
         if (hidden.hidden)
         {
             
             float dist = Vector3.Distance(transform.position, lastPos);
             agent.SetDestination(lastPos);

             if (dist <= .02f)
             {
                 wandering = true;
             }
         }


         /*Otherwise, sets the destination to last known player position, and gets a float for distance from the last seen position. If the distance from the
         last seen position is less than 0.2f, starts the wander loop.*/
         else
         {
             float dist = Vector3.Distance(transform.position, lastPos);
             agent.SetDestination(lastPos);

             if (dist <= .02f)
             {
                 wandering = true;
             }
         }
     }


     //If the distance from the player is less than or equal to the agents stopping distance, sets inAttackRange to true.
     if (distance <= agent.stoppingDistance)
     {
         inAttackRange = true;
     }

     //If the distance from the player is greater than the agents stopping distance, sets inAttackRange to false.
     if (distance > agent.stoppingDistance)
     {
         inAttackRange = false;
     }

     if (wandering)
     {
         agent.SetDestination(wanderTarget.position);
         timer -= Time.deltaTime;
     }

     //If the player is outside of the vision radius, start the wander loop.
     if (distance > vision)
     {
         wandering = true;
     }

     //If the timer is less than or equal to 0, get a new wander destination, and start the timer again.
     if (timer <= 0f)
     {
         wanderTarget.position = new Vector3(Random.Range(-mapBorderX, mapBorderX), 0f, Random.Range(-mapBorderY, mapBorderY));
         timer = Random.Range(1.81f, 2.61f);
     }
 }
 
 //Display the vision radius in the editor.
 void OnDrawGizmosSelected()
 {
     Gizmos.color = Color.black;

     Gizmos.DrawWireSphere(transform.position, vision);
 }

}

The bug: https://youtu.be/IcJAjFSQFec

Once again, thanks to whoever helps me with this, I'm very thankful!

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
Best Answer

Answer by endasil_unity · Dec 29, 2021 at 05:07 PM

When asking for help you can help us understand your problem even better if you describe what steps you have taken to narrow down your issue and try to figure out what is going on.

The first thing I would do is add a bunch of Debug.Log() to narrow down the problem area.

  1. Does wandering get set to true when the enemy stops? Does the enemy actually get within 0.02 meter from lasPos?

  2. Does the timer get down to 0 so it can set a wandering target?

  3. Is the target set a place that is possible for the navmesh agent to travel to?

This will help narrow down where things go wrong so we have an idea what to look for the thing that goes wrong.

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 CeratoRaptor · Dec 29, 2021 at 08:41 PM 0
Share

So I ended up solving the problem myself, turns out the agent's stopping distance was more than the distance from lastPos, so it couldn't reach it. Thank you so much anyways!

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

212 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

Related Questions

Need to walk idly around unless ball is around 1 Answer

Random Wander Ai 0 Answers

Wander AI Mecanim Help 0 Answers

NPC code, works great, besides them being in the ground. 2 Answers

AI teleporting 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