死亡动画播放后,下次射击之前消失
MonsterManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterManager : MonoBehaviour
{
//3.定义动画
private Animation anim;
//5.定义两个动画状态
public AnimationClip idleClip;
public AnimationClip dieClip;
private void Awake()
{
//4.获得动画组件
anim = gameObject.GetComponent<Animation>();
//6.赋值动画
anim.clip = idleClip;
}
//1.检测是否被子弹打中
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag == "Bullet")
{
//2.销毁子弹
Destroy(collision.collider.gameObject);
//7.切换动画
anim.clip = dieClip;
anim.Play();
//10.关闭组件
gameObject.GetComponent<BoxCollider>().enabled = false;
StartCoroutine("Deactivate");
}
}
//8.当怪物disable的时候,将默认的动画修改为idle动画
private void OnDisable()
{
anim.clip = idleClip;
}
//9.迭代器
IEnumerator Deactivate()
{
//两次射击间隔为1s 所以这里时间小于1s
yield return new WaitForSeconds(0.8f);
//使当前的怪物变为未激活状态,并使整个循环重新开始。TargetManager.cs
TargetManager._instance.UpdateMonsters();
}
}
TargetManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetManager : MonoBehaviour
{
//16.方便其他文件调用
public static TargetManager _instance;
//1.获取我们设置的四种怪物:控制怪物的生成或销毁(显示或隐藏) 最开始是都不显示
//2.建立数组
public GameObject[] monsters;
//6.获得激活状态的怪物
public GameObject activeMonster = null;
//17.赋值
private void Awake()
{
_instance = this;
}
//9.调用测试
private void Start()
{
//10.遍历初始化目标怪的状态以及boxcollider状态
foreach (GameObject monster in monsters)
{
monster.GetComponent<BoxCollider>().enabled = false;
monster.SetActive(false);
}
//ActiveMonster();
//12.调用迭代器方法:先注释上一句的直接调用
StartCoroutine("AliveTimer");
}
//3.是否激活各种状态函数
private void ActiveMonster()
{
//4.随机激活:得到index
int index = Random.Range(0, monsters.Length);
//5.激活怪物:需要先获得激活状态的怪物
//赋值
activeMonster = monsters[index];
//7.激活
activeMonster.SetActive(true);
//8.激活box collider
activeMonster.GetComponent<BoxCollider>().enabled = true;
//14.调用
StartCoroutine("DeathTimer");
}
//11.协程控制迭代器 设置怪物生成的等待时间
IEnumerator AliveTimer()
{
yield return new WaitForSeconds(Random.Range(1, 5));
ActiveMonster();
}
//14.控制死亡迭代器 使激活状态的怪变为未激活状态
private void DeActiveMonster()
{
if (activeMonster != null)
{
activeMonster.GetComponent<BoxCollider>().enabled = false;
activeMonster.SetActive(false);
activeMonster = null;
}
//14.再次激活
StartCoroutine("AliveTimer");
}
//设置死亡时间
IEnumerator DeathTimer()
{
yield return new WaitForSeconds(Random.Range(3, 8));
DeActiveMonster();
}
//15.更新怪物 在其他脚本中调用此方法
public void UpdateMonsters()
{
if (activeMonster != null)
{
//关闭所有协程
StopAllCoroutines();
activeMonster.SetActive(false);
activeMonster = null;
StartCoroutine("AliveTimer");
}
}
}
标签:unity3D,动画,activeMonster,void,09,private,怪物,public
From: https://www.cnblogs.com/flyall/p/17179240.html