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

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.

主站蜘蛛池模板: 平果县| 彭水| 曲阳县| 贵溪市| 杭锦后旗| 中超| 锦屏县| 富裕县| 陆丰市| 滨海县| 宜州市| 武宁县| 五大连池市| 博湖县| 门头沟区| 银川市| 平顺县| 桂东县| 醴陵市| 夏河县| 西青区| 安吉县| 威海市| 阳谷县| 济南市| 黄山市| 松桃| 搜索| 策勒县| 晋宁县| 蓬安县| 兖州市| 内乡县| 涪陵区| 西林县| 定陶县| 鸡西市| 深泽县| 吴忠市| 固镇县| 裕民县|