- Home /
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?
Well, since not all of us use RAIN
, we can't give you RAIN-specific help. But everything should be in the documentation, read it well. If nothing's there, maybe you should even send a msg to the author. Lurk around the script it self, try to figure out how it works. I also think you should ask in a separate question, it would be better.
Answer by vexe · Sep 26, 2013 at 09:46 AM
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:
First thank you. The problem is when I use gameObjects our mazes are often realy big so it needs a lot of objects. But if there's no other option we have to do it that way. I'm trying it out with the distance variable.
If you have a maze, then going randomly between nodes -in a basic fashion- isn't going to work. What kind of maze do you have? You might be looking for a maze-solving algorithm in that case.
The player has to get out of the maze befor an enemy hits him. The enemys just have to follow the player and move randomly.
Please give us more info. What code is there currently for the enemy AI? - how is he spotting you? - you said he doesn't stop when he sees you, did you write anything for him to stop?
No i don't have any code to let him stop. As i said at the moment I'm using the behavior tree system of RAIN indie to let him detect the player. So he knows when he can see the player. There's a sensor which marks the player as targeted and then let the enemy follow the target. Now the enemy doesn't need to stop walking he just has to get in the state random walk.
Answer by SAE_Superman · Oct 07, 2013 at 06:29 PM
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.
Your answer
Follow this Question
Related Questions
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers
How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers
How to make basic AI in a 2d game? 4 Answers
Unity 3D Audio Clip Disabled 1 Answer