So I'm creating a zombie game where the zombies are passive and just walk randomly all over the area when player is not near. My question is: how would I make zombies move randomly? I code in C#. Basically, there is a town filled with zombies that just wander around the area (walk randomly everywhere slowly) until the player is seen or is very near. Thanks.
-
4\$\begingroup\$ It's a steering behaviour named wander. \$\endgroup\$– Vaillancourt ♦Dec 15, 2015 at 18:20
-
\$\begingroup\$ Can you provide a more clear picture of your scenario? \$\endgroup\$– Hamza HasanDec 15, 2015 at 19:17
-
\$\begingroup\$ There is a town filled with zombies that just wander around the area (walk randomly everywhere slowly) until the player is seen or is very near. \$\endgroup\$– John SmithDec 15, 2015 at 19:19
-
\$\begingroup\$ docs.unity3d.com/ScriptReference/Random-insideUnitCircle.html \$\endgroup\$– starikcetinDec 15, 2015 at 19:33
-
\$\begingroup\$ Sounds like a job for steering behaviours (Wander/Seek/Flee) \$\endgroup\$– MattiasDec 15, 2015 at 19:42
2 Answers
You would need to implement two AI states: Seek & Wander The zombies will have to have an attention radius.
if player is within the attention radius
seek(player)
else
wander()
In each wander behaviour you will want to have a location for it to wander to. If you have a complex area you would want to have the zombie follow a specified path. Which I would suggest to use A* to create the paths.
function wander()
if path is empty
findPath(randomPoint)
else
seek(path.NextNode.position)
To be able to steer towards a point you want to use the dot product. Here is what I use to steer towards a point:
function seek(point)
fwd = normalize(getForwardVector())
diff = normalize(point - this.position)
dot = dot(fwd, diff)
this.rotationalVelocity.y = dot * this.rotationalSpeed;
this.velocity = fwd * this.wanderSpeed;
If you want to make the zombies even more dumb you could test if the dot
product of the fwd, and diff vectors are greater than 0. That means the zombie is facing the player. If it less than or equal to 0 then don't seek the player.
For random movement, define variable radius, which is the greatest distance that the zombie can go at a moment, and some sort of time variable which is how long they will stand there. Generate a vector with distance less than radius, and angle of which you want the zombie to move. Then, wait a random amount of time, and repeat. Of course, this is a very simple and "stupid" ai. Like mentioned above, A* would probably be the way to go with randomly generated obstacles to make paths.
Good luck