目录
20、僵尸攻击
僵尸攻击动画导入




开始在写僵尸攻击的主逻辑,在Zombie.cs中更新代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zombie : MonoBehaviour
{
……
// 是否在攻击中
private bool isAttackState;
// 攻击力
private float attackValue = 100;
……
/// <summary>
/// 僵尸移动
/// </summary>
private void Move()
{
// 没有得到网格,没有僵尸
if (currGrid == null) return;
// 如果在攻击中也跳过移动检测
if (isAttackState) return;
currGrid = GridManager.instance.GetGridByWorldPos(transform.position);
// 当前网格中有植物 并且 它在僵尸的左边 且 距离很近
if (currGrid.HavePlant && currGrid.CurrPlantBase.transform.position.x < transform.position.x
&& transform.position.x - currGrid.CurrPlantBase.transform.position.x < 0.3f)
{
// 开始攻击植物
Attack(currGrid.CurrPlantBase);
}
// -1.33f是一个网格的距离,所以-1.33f*(1/speed)等于每秒走了1.33f*(1/speed)的距离【速度】,然后乘上时间Time.deltaTime,就是每帧走的距离
transform.Translate(new Vector2(-1.33f,0)*(1/speed)*Time.deltaTime);
}
private void Attack(PlantBase plant)
{
isAttackState = true;
// 自身播放攻击动画
animator.Play("Zombie_Attack");
// 植物相关逻辑
StartCoroutine(DoHurt(plant));
}
/// <s