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.
using UnityEngine;
using System.Collections;
public class EnemyTriggers : MonoBehaviour {
public GameObject player;
public EnemySight[] enes;
public NavMeshAgent nav;
bool playerEnter;
// Use this for initialization
void Start () {
for (int i=0; i < enes.Length; i++)
{
enes.Patrolling(); //here is where I am stuck. What should go here instead?
playerEnter = false;
}
}
void OnTriggerEnter (Collider col)
{
if (col.tag == "Player")
{
for (int i = 0; i < enes.Length; i++)
{
playerEnter = true;
}
}
}
}
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!
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).
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.
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.
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.
tree "root"
fallback
tree "Chase"
tree "Patrol"
tree "Chase"
sequence
IsPlayerInSameRoom
while PlayerWithinChaseRange
MoveTo_Player
tree "Patrol"
while not IsPlayerInSameRoom
MoveTo_NexWayPoint
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.