float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);//沿z轴移动
public class AutoDestoryComponent : MonoBehaviour
{
#region ICanCache
public ParticleSystem[] m_pss = null;
public int m_life = 1; //3条命
public float m_AutoDeadTime = 3;//3s自动销毁
private int m_life_Base = 3; //3条命【恢复用】
private float m_AutoDeadTime_Base = 3;//3s自动销毁【恢复用】【-1:表示不自动销毁,如Enemy】
void Update()
{
//需要自动销毁
if (m_AutoDeadTime_Base >= 0)
{
m_AutoDeadTime -= Time.deltaTime;//每一帧减去一个Time.deltaTime
if (m_AutoDeadTime <= 0)
{
InnerDead(); // 物体销毁
return;
}
}
if (m_life <= 0)
{
InnerDead(); //玩家死亡
}
}
/// <summary>
/// 设置自动销毁数据
/// </summary>
/// <param name="life_base">默认生命值</param>
/// <param name="autoDeadTime_base">-1不自动销毁;其他数据代表销毁时间(单位s)</param>
public void SetBasePara(int life_base = 1, float autoDeadTime_base = -1)
{
m_AutoDeadTime = m_AutoDeadTime_Base = autoDeadTime_base; //用来恢复时间
m_life = m_life_Base = life_base; //用来恢复初始生命
}
//是否启用
public bool IsUse { get; set; }
//死后位置
public Vector3 DeathPosition
{
get
{
return new Vector3(2000, 2000, 2000); //返回新位置Vector3(x,y,z)
}
}
//复活
public void Init(Vector3 position, Quaternion rotation)
{
transform.gameObject.SetActive(true);
transform.position = position;
transform.rotation = rotation;
IsUse = true;
foreach (ParticleSystem item in m_pss)
{
item.Play(true);
}
//有些绕
m_life = m_life_Base;
m_AutoDeadTime = m_AutoDeadTime_Base;
}
private void InnerDead()
{
IsUse = false;
transform.position = DeathPosition;
foreach (ParticleSystem item in m_pss)
{
item.Stop(true);
}
this.gameObject.SetActive(false);
}
#endregion
}