官术网_书友最值得收藏!

Defining the states

Let's define what triggers each state and how the AI should choose the correct state in different scenarios. The PASSIVE state will be the default state, and the game will start in that position until the player comes across our character. The DEFENSIVE state will be used in two different situations–if the player comes from the right side and if he has already confronted the player and has low HP. Finally, the AGGRESSIVE state will be activated if the player comes from the left side or has already arrived at the middle area:

public class Enemy : MonoBehaviour {
private int Health = 100;
private bool statePassive;
private bool stateAggressive;
private bool stateDefensive;
private bool triggerL;
private bool triggerR;
private bool triggerM;
// Use this for initialisation
void Start () {
statePassive = true;
}
// Update is called once per frame
void Update () {
// The AI will remain passive until an interaction with the player occurs
if(Health == 100 && triggerL == false && triggerR == false && triggerM
== false)
{
statePassive = true;
stateAggressive = false;
stateDefensive = false;
}
// The AI will shift to the defensive mode if player comes from the
right side or if the AI is below 20 HP
if(Health<= 100 && triggerR == true || Health<= 20)
{
statePassive = false;
stateAggressive = false;
stateDefensive = true;
}
// The AI will shift to the aggressive mode if player comes from the
left side or it's on the middle and AI is above 20HP
if(Health> 20 && triggerL == true || Health> 20 && triggerM == true)
{
statePassive = false;
stateAggressive = true;
stateDefensive = false;
}
}
}

We added the trigger variables triggerL, trigger, and triggerM and also defined when the AI should change from one behavior state to another. At this point, our enemy already knows what to do in different situations that may occur during gameplay according to the player's position.

Now we just need to determine what will happen on each state, because that is what differentiates a DEFENSIVE state from an AGGRESSIVE one. For this specific enemy, where his main function is to protect the entrance of the building, we want him to stay put at all times and to never run after the player. This is because the AI doesn't know that it is just one person and can't take the risk of going after just one soldier if there's a possibility of being greeted by several. This will help to give a little realism to the actions of the enemy. We'll also use the defensive behavior state for the moment where the enemy feels that it is losing the battle and is about to die, or when it has the advantage of using the building to protect itself while the player doesn't. Finally, the aggressive state will be used when the AI sees a clear advantage to kill or if it doesn't have any other options.

主站蜘蛛池模板: 连云港市| 汕尾市| 五原县| 仲巴县| 崇义县| 龙里县| 荣成市| 于田县| 湘乡市| 遵义市| 金昌市| 广西| 兴宁市| 茂名市| 义马市| 上杭县| 柘荣县| 秦皇岛市| 新化县| 德安县| 古交市| 泾川县| 通江县| 湖北省| 双峰县| 东丰县| 邮箱| 炉霍县| 莱阳市| 霞浦县| 临夏市| 新乡市| 屯门区| 汤阴县| 都江堰市| 小金县| 寿阳县| 十堰市| 秦安县| 横峰县| 奇台县|