OnTriggerStay 仅适用于一个对象

时间:2021-04-07 06:04:16

标签: c# unity3d

我正在开发一个 AI 战斗系统,其中每个 AI 都有一个启用了“触发器”的辅助对撞机。到目前为止,这是我的脚本

    public float health = 100;
 public int isrunning = 1;
 public GameObject currenttarget;
 public int attackspeed;
 public int damage;
 public int newdamage = 0;
 void Start()
 {
     StartCoroutine(DoDamage());
     
 }

 public void TakeDamage(float x)
 {
     this.health = this.health - x;
 }

 public IEnumerator DoDamage()
 {
     isrunning = 0;
     yield return new WaitForSeconds(attackspeed);
     Debug.Log("loop");
     newdamage = damage;
     isrunning = 1;
 }



 private void OnTriggerStay(Collider other)
 {
     if ( other.gameObject.CompareTag("AI"))
         {
         other.GetComponent<Framework>().TakeDamage(newdamage);
         newdamage = 0;
     }

 }

 private void Update()
 {
     if (isrunning==1)
     {
         StartCoroutine(DoDamage());
     }
 }


 // Update is called once per frame

}

当我用这个脚本放置三个对象时,伤害设置为 5,攻击率设置为 1,我想要的结果是:A.100 B.100 C.100(1 秒)A。 80 B.80 C.80 但是我发现 other.GetComponent().TakeDamage 一次只应用于一个对象,而不是根据我的需要应用于其他两个对象。这是 OnTriggerStay 应该如何工作吗?如果是这样,是否有任何解决方法?

1 个答案:

答案 0 :(得分:0)

以我的方式,我建议你使用一个对象列表,每当敌人攻击(OnTriggerEnter)玩家时,你可以将它们推送到列表中,然后使用Update Func中的foreach循环来做一些像take这样的函数伤害,...,敌人停止攻击后(OnTriggerExit)可以将其从列表中弹出。

相关问题