Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enemy AI NavMesh flee

Discussion in 'Navigation' started by Deleted User, Oct 7, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hey, guys. I'm making a horror game where the enemy AI has two states: Chase and Hide. In the Chase state, my enemy is supposed to find, run at, and attack the player. In the Hide state, my enemy is supposed to flee away from the player. I'm having some trouble making this state. Does anybody know how I can get my enemy AI to flee from the player to a random waypoint and transition back to the Chase state after a duration of time? If so, I'd greatly appreciate if someone could show me how I can get my enemy AI to do so. Please and Thank You!

    ~Darkwood Productions
     
  2. TWicked

    TWicked

    Joined:
    Nov 8, 2013
    Posts:
    14
    2 states for AI Chase and Hide
    1 - Chase
    Set NavMeshAgent.destination to player
    use If statement for what ever condition you have in mind to switch state to Hide.
    2 - Hide.
    Few ways of doing this you can predefine few waypoint for Ai where AI can hide.
    basically then you would create an array with all waypoint.
    from there randomly pick one and set NavMeshAgent.destination as it
    Alternatively you can find closes waypoint to AI and set NavMeshAgent.destination as it.
     
  3. Invictus_

    Invictus_

    Joined:
    Jul 30, 2015
    Posts:
    19
    sorry to jump on your post.

    how would you find the closest waypoint to the navmesh agent?
     
  4. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class JBR_CheckDistance : MonoBehaviour {
    5.  
    6. public NavMeshAgent agent;
    7. public float farthestWaypointDistance = 500; // only use waypoint with in this range
    8. public float closestDistance;
    9. public int wayPointRef;
    10. public float updateSpeed = 0.1f; // how many times the code will be called per second .1 = 10 times
    11.  
    12. void Start(){
    13. //no need to check every frame
    14.   InvokeRepeating("WaypointCheck", updateSpeed, updateSpeed);
    15. }
    16. void WaypointCheck(){
    17. //find all waypoints note: all waypoints need to have their tag set.
    18. Gameobject[] waypoints = gameobject.findgameobjectswithtag("Waypoints");
    19. //reset distance so that all waypoints are used in check
    20. closestDistance = farthestWaypointDistance;
    21. for(int i = 0; i < waypoints.length; i++)
    22. {
    23. float dist = Vector3.Distance(this.gameobject.transform.position, waypoints[i].transform.position);
    24. If(dist < closestDistance){
    25. closestDistance = dist;
    26. wayPointRef = i;
    27. }
    28. }
    29. SetNewDestination(waypoints[wayPointRef]);
    30. }
    31.  
    32. void SetNewDestination(Gameobject waypoint){
    33. agent.SetDestination(waypoint.transform.position);
    34. }
    35. }
    Try this i did it on my phone so might be a few minor misSpelling...
    edit: added a few updates so that there shouldnt be any errors.
     
    Last edited: Oct 10, 2016
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    @JBR-games Thanks, man! This will go perfectly with the enemy AI script that I'm building.
     
  6. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    glad to help, added a few more features.
     
  7. Invictus_

    Invictus_

    Joined:
    Jul 30, 2015
    Posts:
    19
    using "gameobject.findgameobjectswithtag("Waypoints");" is expensive to do in the function, run it in the start or use a public array of gameobjects and put them in that way. just more efficient.
     
  8. vulgerstal

    vulgerstal

    Joined:
    Jun 26, 2014
    Posts:
    9
    1. Create an empty Game Object, call it "fleeArea";
    2. Add Navigation Mesh Obstacle to it, adjust its size to size of area the enemy would flee;
    3. Add the to player following script
    Code (CSharp):
    1.        
    2.  
    3. public float Camera; //player's camera
    4. public float range = 25; //player's sight distance
    5.  
    6. void Start() {}
    7. void Update ()
    8. {
    9. RaycastHit hit;
    10.         Vector3 rayDirection = camera.gameObject.transform.forward;
    11.         if (Physics.Raycast (transform.position, rayDirection, out hit, range))
    12.         {
    13.  
    14.             if (hit.collider.GetComponent<Enemy> ()) { //If we see enemy
    15.                 fleeArea.transform.position = hit.point;
    16.                 fleeArea.GetComponent<NavMeshObstacle>().enabled = true; //He/she flees
    17.             }
    18.             else fleeArea.GetComponent<NavMeshObstacle>().enabled = false; //If we don't see enemy. She/he returns.
    19. }
    20.  
    21.         }