Jump to content
  • entries
    941
  • comments
    5,894
  • views
    867,913

Building the Soldier AI


Josh

2,292 views

 Share

I recently added a new "SoldierAI" script to handle enemies that can shoot back and coordinate with one another. This was developed for the new Mercenary Action Figure. In this blog I will explain how I designed the behaviors to work.

 

Just like the MonsterAI script, the soldier AI script includes a "teamid" value. This has three possible values, "good", "bad", and "neutral". Good guys and bad guys will attack each other, but not their own team or neutral characters.

 

blogentry-1-0-08740900-1455637717_thumb.jpg

 

At its simplest, the AI I wanted was similar to the old enemies in the original Quake game. When you are far away, they shoot at you. When you are close enough, they will stab / claw / chainsaw you with a melee attack. This was easy to implement by adjusting behavior based on distance. Instead of making the soldiers stand there shooting in bursts, I decided to make them run forward in between shots, until they reach an optimum shooting distance. They will also move towards the player, using the navmesh, if their view of the player is blocked, as you can see in the end of this video when the player hides behind a truck:

 

 

The Soldier AI has two ways of identifying an enemy. (In the script, this value is called "target".) They may visually sense the enemy in front of them, or hear them if they are close enough. They may also begin attacking if they receive damage from an enemy. Even if they are already chasing one enemy, if another enemy attacks them and is closer than their target, they will switch to fight the new attacker. I found this necessary when I saw a soldier shooting at a distant crawler, while another one right next to him was clawing at him.

 

 

When a soldier is alerted to the presence of an enemy, all characters on his team within a certain distance will also become aware of the enemy. This means that if one soldier sees you, the whole pack will come looking for you, so you'd better be prepared!

 

Finally, the bullets the soldier shoots are managed by a new "Projectile" script and a "tracer" prefab. The script is written in a general-purpose way so that it can also handle arrows, rockets, and other types projectiles. This will allow the soldier AI to work with a variety of characters with different types of weapons.

 

To make the soldier AI script work with any character model, you just need the following animations:

  • Death
  • Fire
  • Hit
  • Idle
  • Attack1
  • Attack2 (optional)
  • Run
  • Walk

 

You also need a child limb named "muzzle" in the location where you want bullets to emit from.

  • Upvote 7
 Share

4 Comments


Recommended Comments

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...