今天做了一天 做出来了怪物的攻击死亡动画和掉落金币 代码如下:
public static int life = 5; //怪物的生命值
public GameObject mouse; // 定义怪物
private NavMeshAgent agent; //定义组件
public Transform hero; //定义一个人物来杀死怪物
public GameObject gold; // 定义金币
void Start()
{
agent = GetComponent<NavMeshAgent>();//添加组件
}
void Update()
{
if (hero != null)
{
if (Vector3.Distance(transform.position, hero.position) < 2f) //当人物靠近怪物2M之内时
{
if (life > 0)
{
AnimationToWorking(); //做攻击动画
}
else if (life <= 0)
{
AnimationToDie();
// Destroy(this.gameObject, 1);
}
}
else if (Vector3.Distance(transform.position, hero.position) > 4f) //当人物的距离在怪物4M之外时
{
AnimationToIdle(); //做停止动画
agent.SetDestination(transform.position);
}
else if (Vector3.Distance(transform.position, hero.position) < 4f) //当人物在怪物4M之内时
{
AnimationToWalk(); //怪物追击人物
agent.SetDestination(hero.position);
}
}
else
{
AnimationToIdle();
}
}
public void AnimationToWorking()
{
mouse.transform.animation.Play("Ratkin_1H_Heavy Smash"); //攻击动画
}
public void AnimationToWalk()
{
mouse.transform.animation.Play("Ratkin_1H_Casual_walk"); // 追击动画
}
public void AnimationToDie()
{
mouse.transform.animation.Play("Ratkin_1H_Dying_B"); //死亡动画
}
public void AnimationToIdle()
{
mouse.transform.animation.Play("Ratkin_IDLE"); //停止动画
}
void OnTriggerEnter(Collider other)
{
//被子弹打中耐力小于零死亡
if (other.CompareTag("Bullet"))
{
life--;
print(life); //当怪物碰到标签为 Bullet时 生命值减少
if (life <= 0)
{
Destroy(this.gameObject, 1);
Instantiate(gold, transform.position, Quaternion.identity);//死后产生金币
//经验增加
}
}
}