Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的函数在OnTiggerStay中运行两次?_C#_Unity3d_Collision - Fatal编程技术网

C# 为什么我的函数在OnTiggerStay中运行两次?

C# 为什么我的函数在OnTiggerStay中运行两次?,c#,unity3d,collision,C#,Unity3d,Collision,我有一个简单的统一游戏 这种情况就像我杀死了一个敌人,它会掉落一个可拾取的生命值。 在OnTriggerEnter中,我处理玩家与生命值的冲突,如果玩家失去生命值,它会恢复一定数量的生命值。(准确地说是70) 当玩家在最大生命值时站在生命值上,它什么也不做。但是如果玩家站在生命值上时受到伤害,生命值也应该被恢复,这就是OnTriggerStay的作用 在那里,我经常检查玩家是否需要倾斜,如果需要,进行治疗,并摧毁治疗对象。 我的问题是它运行了两次。它会对玩家治疗两次。无论我做什么,我在OnTri

我有一个简单的统一游戏

这种情况就像我杀死了一个敌人,它会掉落一个可拾取的生命值。 在OnTriggerEnter中,我处理玩家与生命值的冲突,如果玩家失去生命值,它会恢复一定数量的生命值。(准确地说是70)

当玩家在最大生命值时站在生命值上,它什么也不做。但是如果玩家站在生命值上时受到伤害,生命值也应该被恢复,这就是OnTriggerStay的作用

在那里,我经常检查玩家是否需要倾斜,如果需要,进行治疗,并摧毁治疗对象。 我的问题是它运行了两次。它会对玩家治疗两次。无论我做什么,我在OnTriggerStay中调用的函数都将运行两次。为什么呢?有人知道解决办法吗

以下是我的Player.cs文件和我的Heal.cs文件的一部分:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
  //HP and MANA system
    [SerializeField]
    private int playerHealth;
    [SerializeField]
    private int playerMaxHealth = 100;
    [SerializeField]
    private Transform Healthbar;

    private void OnTriggerStay(Collider other)
    {
        if (other.tag == "Enemy")
        {
            timer += Time.deltaTime;
            if (timer > attackSpeed)
            {
                Enemy enemy = other.GetComponent<Enemy>();
                if (enemy != null)
                {
                    enemy.DamageEnemy(playerDamage);
                }
                timer = 0.0f;
            }
        }
        if (other.tag == "PatrollingEnemy")
        {
            timer += Time.deltaTime;
            if (timer > attackSpeed)
            {
                PatrollingEnemy enemy = other.GetComponent<PatrollingEnemy>();
                if (enemy != null)
                {
                    enemy.DamageEnemy(playerDamage);
                }
                timer = 0.0f;
            }
        }
        if (other.tag == "Heal")
        {
            heal(other, false);

        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Heal")
        {
            heal(other, false);
        }
        else if (other.tag == "HealthPotion")
        {
            heal(other, true);
        }
    }
    private void heal(Collider other, bool isPotion)
    {

        dynamic heal;
        if (isPotion)
        {
            heal = other.GetComponent<HealthPotion>();
        }
        else
        {
            heal = other.GetComponent<Heal>();
        }
        if (playerHealth < playerMaxHealth && heal != null)
        {
            addHealthToPlayer(heal.HealAmount);
            Destroy(other.gameObject);
        }
    }

    private void addHealthToPlayer(int amount)
    {

        playerHealth += amount;
        if (playerHealth > playerMaxHealth)
        {
            playerHealth = playerMaxHealth;
        }
        rescaleHealthBar();
    }

    private void rescaleHealthBar()
    {
        Healthbar.transform.localScale = new Vector3((float)playerHealth / (float)playerMaxHealth, 1.0f, 1.0f);
    } 
}


public class Heal : MonoBehaviour
{
    float timer = 0.0f;
    float destroyTimer = 0.0f;
    float timeWhenDestroy = 15f;
    Vector3 rotation = new Vector3(0, 45, 0);

    private int healAmount = 70;
    public int HealAmount
    {
        get
        {
            return healAmount;
        }
    }

    void Update()
    {
        timer += Time.deltaTime;
        destroyTimer += Time.deltaTime;
        if (destroyTimer > timeWhenDestroy)
        {
            Destroy(this.gameObject);
        }
        transform.Rotate(rotation * Time.deltaTime);
        if (timer < 1.0f)
        {
            transform.Translate(Vector3.up * Time.deltaTime);
        }
        if (timer > 1.0f)
        {
            transform.Translate(-Vector3.up * Time.deltaTime);
        }
        if (timer > 2.0f)
        {
            timer = 0.0f;
        }
    }
}
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家:单一行为
{
//HP和MANA系统
[序列化字段]
私人娱乐健康;
[序列化字段]
私人int playerMaxHealth=100;
[序列化字段]
私人健身吧;
私人空间OnTriggerStay(碰撞器其他)
{
if(other.tag==“敌人”)
{
timer+=Time.deltaTime;
如果(计时器>攻击速度)
{
敌方=其他.GetComponent();
如果(敌人!=null)
{
敌人。伤害敌人(玩家伤害);
}
定时器=0.0f;
}
}
如果(other.tag==“PatrollingNemy”)
{
timer+=Time.deltaTime;
如果(计时器>攻击速度)
{
PatrollingNemy敌军=other.GetComponent();
如果(敌人!=null)
{
敌人。伤害敌人(玩家伤害);
}
定时器=0.0f;
}
}
如果(other.tag==“Heal”)
{
治愈(其他,错误);
}
}
专用空对撞机(对撞机其他)
{
如果(other.tag==“Heal”)
{
治愈(其他,错误);
}
else if(other.tag==“HealthPotion”)
{
治愈(其他,真实);
}
}
专用虚空治疗(碰撞器其他,布尔isPotion)
{
动态愈合;
如果(isPotion)
{
heal=other.GetComponent();
}
其他的
{
heal=other.GetComponent();
}
if(playerHealthplayerMaxHealth)
{
playerHealth=playerMaxHealth;
}
重新缩放HealthBar();
}
私有void rescaleHealthBar()
{
Healthbar.transform.localScale=新矢量3((浮点)playerHealth/(浮点)playerMaxHealth,1.0f,1.0f);
} 
}
公共阶层:单一行为
{
浮动定时器=0.0f;
浮动定时器=0.0f;
当温度=15f时的浮动时间;
矢量3旋转=新矢量3(0,45,0);
私有整数healAmount=70;
海拉蒙特公共酒店
{
得到
{
返回healAmount;
}
}
无效更新()
{
timer+=Time.deltaTime;
destroyTimer+=Time.deltaTime;
如果(destroyTimer>timeWhenDestroy)
{
摧毁(这个游戏对象);
}
transform.Rotate(旋转*时间deltaTime);
中频(定时器<1.0f)
{
transform.Translate(Vector3.up*Time.deltaTime);
}
如果(计时器>1.0f)
{
transform.Translate(-Vector3.up*Time.deltaTime);
}
如果(计时器>2.0f)
{
定时器=0.0f;
}
}
}

可能您的两个事件(进入和停留)都在该帧期间触发

OnTiggerStay几乎被称为每个碰撞器的所有帧,其他碰撞器接触到触发器。该函数位于物理计时器上,因此它不一定每帧都运行


OnTiggerStay在多个帧上工作,添加一个在收集时更改的bool,例如:
if(!collected){collected=true;}
执行这样的标志检查将停止它,但如果玩家获得生命值,则治疗对象将被销毁。当对象不再存在时,它是如何进行治疗的?