Make AI walk around randomly until Player is Seen. (C#)

I have managed to make my AI follow me around as soon as he sees me but he doesn’t stop when I get out of sight. I also want the enemy (AI) walk around randomly before he sees me and start walking around randomly again when I get out of Sight. Everything is in a maze so the enemy needs to know where the walls are so he doesn’t walk trough them.

At the moment I’m using the behavior tree system of RAIN indie to let the AI follow the player.

Here’s what I’ve got so far:

<behaviortree name="Enemy" precondition="" repeatuntil="" debug="False"><sequencer name="root" precondition="" repeatuntil="" debug="True"><detect name="detect 241" precondition="" repeatuntil="" debug="True" sensor="sight" aspect="target" variable="playerpos" /><move name="move 2403" precondition="" repeatuntil="" debug="True" movetarget="playerpos" movespeed="-1" looktarget="playerpos" lookspeed="-1" animation="" animationlayer="0" animationwrapmode="default" animationbasespeed="2" /></sequencer><variable name="CanSee" initialvalue="0" /></behaviortree>

I would also apreciate if anyone could tell me how to do this without using an asset and just a script.
Thanks for your Help.

I’m trying to do this now with a script. It’s like that:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
	
	private GameObject Player;
	private bool CanSee = false;
	
	void update()
	{	if(CanSee=true)
		{
			follow();
		}
		else{
			random();
		}
	}
	
	private void follow()
		{	//lets the enemy follow the player
			RAIN.Path.MoveLookTarget.SaveLastPosition(Player);

		}
	
	private void random()
	{	//lets the enemy move randomly
		
	}
}

Of course it knows now when it can see the player. Now I’m trying to let it walk to the player.

EDIT: I’ve found now a class called RAIN.Motion.Instantaneous.SB_Wander it should let an object wander around. But I don’t know how to use this in a script. Can someone help me?

I don’t know about RAIN. But for randomly walking around, you could just create a couple of empty gameObjects pre-set their positions in your scene and maybe let your enemy store them in an array or list at his disposal and choose randomly between them (depends on what ‘random’ means to you)

nextNode = nodes[Random.Range(0, nodes.Length)];
  • Careful with Random.Range though, in the int version
    the beginning limit is inclusive and the end is exclusive. See.
  • Of course, selecting a node randomly
    and heading to it in a trivial way
    isn’t very sufficient, you gotta have
    some functions/methods for him to
    help him find his path, what if for
    example two nodes weren’t exposed?
    (walls between them or something)…
    going into other deep waters there.

About stopping when he sees you, I don’t know how you set him up to detect you, but it should be done via a distance variable that you set, and a field of view angle. So if you’re in that distance && inside that fov he sees you, otherwise not and go back to his ‘idle’ state. Btw you should have states for your enemy stored as enums - Attacking, idle, Spectating, etc.

Couple of helpful links:

  • Here’s a very nice article about
    field of view and dot product.
  • Here’s a very nice tutorial on
    AI and enemy states.

Collision detection is your friend. Simply add a trigger collision volume to your characters. This way you can detect when characters enter, stay in or exit that “field of view”. Inside the OnEnter, turn on the follow behavior and inside the OnExit, turn on the wander behavior.

Being a maze, you might be able to “see through walls”, but you can fix this by having a unique ID for each “corrider” or “hallway” and do a simple ID check to see if who you are seeing is in the same hallway. There are other ways (better but also more complex) to fix this issue too though.