【发布时间】:2020-09-14 17:08:19
【问题描述】:
所以我有一个 OnTriggerEnter 函数,它使用协程每秒删除一次生命值。我想用 OnTriggerExit 函数和 StopCoroutine 来停止它。虽然这只适用于一个游戏对象,但它会因多个碰撞器而中断,因为它总是取消“例程”。为离开 Collider 的对象停止协程的最佳方法是什么?我觉得有一个简单的解决方案,但我只是没有看到它。
OnTriggerEnter/Exit
private float delay= 1f;
void OnTriggerEnter(Collider other)
{
DealDamage(other, delay);
}
private void OnTriggerExit(Collider other)
{
StopDamage();
}
协程
private Coroutine routine;
private void DealDamage(Collider col, float damageRate)
{
var health = col.GetComponent<IChangeHealth>();
if (health == null) return;
routine = StartCoroutine((DamageEverySecond(health, damageRate)));
}
IEnumerator DamageEverySecond(IChangeHealth health, float rate)
{
while (true)
{
health.RemoveHealth(Damage);
yield return new WaitForSeconds(rate);
}
}
protected void StopDamage()
{
StopCoroutine(routine);
}
【问题讨论】:
-
您可以将协程存储在进入/退出触发器的“其他”对象中。或者您可以将协程保存在字典中,通过其他游戏对象查找。
标签: c# unity3d crontrigger