7.敌人攻击
现在我们希望赋予敌人夺走我们生命的能力。
首先将Enemy Attack拖到可爱的ZomBunny上。
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
bool playerInRange;
float timer;
值得解释的是Timer,Timer是我们用来控制的一个变量,不会让整个的攻击太频繁,也不会太稀松。
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent <PlayerHealth> ();
//enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent <Animator> ();
}
nothing special。
void OnTriggerEnter (Collider other)
{
if(other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if(other.gameObject == player)
{
playerInRange = false;
}
}
ok这个值得写点东西解释下。我们的攻击是按照trigger来进行判定的,它的形参是Collider类的,所以只要有一个可撞击的类,Capsule也好Sphere也好,只要撞进了我的Collider,就进行攻击判断。如果这个撞进来的东西,身上恰好有着player的tag,那么稳了,就是你了弟弟,我们把playerInRange的bool置为真。下面的方法就是说如果出去了,那么我们在把这个值置回来。这两个方法不能缺少一个,因为有可能我们的玩家挂机了,就一直呆在那里任人宰割,我们不能说出于人道主义精神不打了,如果他还在攻击范围内,bool值就仍然是true,就要削他,削稀碎。
void Update ()
{
timer += Time.deltaTime;
if(timer >= timeBetweenAttacks &&a