Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemies chase once player enters room

Discussion in 'Scripting' started by swasfy0, Apr 21, 2016.

  1. swasfy0

    swasfy0

    Joined:
    Feb 6, 2016
    Posts:
    2
    Hello! I am trying to build a level where my player goes down a corridor to go to a room filled with patrolling enemies. The thing is I don't want my enemies to chase down my player before my player walks into the room. My enemies are patrolling inside the room. The way I wrote my enemy movement script (called EnemySight) is there are two waypoints in which they patrol between. Once my player is within range (playerDistance), the enemy stops, looks at the player, and chases him down until the player goes out of range and the enemy resumes patrolling.
    Here's my code but it's buggy. I haven't tested it because it is very incomplete but I'm having a lot of trouble writing it because I am not too familiar with a script being an array.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyTriggers : MonoBehaviour {
    5.     public GameObject player;
    6.     public EnemySight[] enes;
    7.     public NavMeshAgent nav;
    8.     bool playerEnter;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         for (int i=0; i < enes.Length; i++)
    13.         {
    14.             enes.Patrolling();   //here is where I am stuck. What should go here instead?
    15.             playerEnter = false;
    16.         }
    17.     }
    18.  
    19.     void OnTriggerEnter (Collider col)
    20.     {
    21.         if (col.tag == "Player")
    22.         {
    23.             for (int i = 0; i < enes.Length; i++)
    24.             {
    25.                 playerEnter = true;
    26.             }
    27.         }
    28.    
    29.     }
    30. }
    My enemy movement script is called EnemySight. Inside that script is a function called Patrolling() where the enemy patrols on a timer. In that same script, I have a function called Chase() which only has nav.SetDestination(player.position);. Very simple, no?

    My logic is the enemy patrols until my player goes through the trigger which has this script. This script calls EnemySight script which has Chase() and Patrolling(). I've placed it as an array in case I want to have 2+ enemies in one room.

    I really do apologize for the lengthy description. I really hope this makes sense. I would really appreciate help with this!
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Hi,

    You might be interested by Panda BT, it's a script-based Behaviour Tree engine which is intended to make AI writing easier.

    This package contains an example, a 3rd person shooter, which demonstrates behaviours such as: Patrolling, Chasing, Attacking, Running for Cover. The chasing is triggered when the enemies see the player, but I guess it can be easily adapted when the player entered a room (using a room collider).

    Let me know if you have any question about using this package (I'm the author).

    More information here:
    http://www.pandabehaviour.com/
     
  3. swasfy0

    swasfy0

    Joined:
    Feb 6, 2016
    Posts:
    2

    Thanks but I would except I am not too familiar with tree scripting. Plus, this is for a school assignment so I'm not so sure if my professor would allow this.
     
  4. Chaz32621

    Chaz32621

    Joined:
    Apr 17, 2015
    Posts:
    199
    Have it based on the distance the player is from the enemey if so close then chase if far out then dont.

    Or you could do something where when the player goes into the room the player gains a tag or layer and if player has said tag or layer the enemies can follow.
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @swasfy0

    Panda BT uses a minimalist scripting language, far more simple than C#. Though you still need some knowledge about Behaviour Tree (which is not that hard to learn), to understand what the basic nodes (such as sequence, fallback, while, ...) actually do. To give you an example to show how simple it can be, your BT script for your AI would look something like this.
    Code (CSharp):
    1. tree "root"
    2.     fallback
    3.         tree "Chase"
    4.         tree "Patrol"
    5.  
    6. tree "Chase"
    7.     sequence
    8.         IsPlayerInSameRoom
    9.         while PlayerWithinChaseRange
    10.             MoveTo_Player
    11.  
    12. tree "Patrol"
    13.     while not IsPlayerInSameRoom
    14.         MoveTo_NexWayPoint
    15.  
    Note that there are still work to do on the C# sharp side: PlayerWithinChaseRange, MoveTo_Player, IsPlayerInSameRoom and MoveTo_NexWayPoint has to be implemented in a MonoBehaviour as [Task].

    You would indeed run the risk that your professor would not expect this technique for your assignment. However, there is also so possibility you get extra points because your did some extra research on AI techniques. But I would suggest discussing this with your professor first to avoid any bad surprise.
     
    Chaz32621 likes this.
  6. Chaz32621

    Chaz32621

    Joined:
    Apr 17, 2015
    Posts:
    199
    I think his biggest problem is that its for a school project. My professor always forbidden using anyone elses code. Let alone a full scale system.
     
  7. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @Chaz32621 Who are you? What have you done to @swasfy0 ?

    I agree that it might not be what your professor is expecting.
     
  8. Chaz32621

    Chaz32621

    Joined:
    Apr 17, 2015
    Posts:
    199
    I am his father!!!!
     
  9. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    plot twist.
     
    Chaz32621 likes this.