为什么我的函数在OnTriggerStay中运行两次?

问题描述 投票:-1回答:1

我在Unity中有一个简单的游戏。

这种情况就像我杀死敌人一样,它会掉落可拾取的生命。在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;
        }
    }
}
c# unity3d collision
1个回答
0
投票

https://docs.unity3d.com/ScriptReference/Object.Destroy.html

也许您的两个事件在该帧期间都被触发了?

© www.soinside.com 2019 - 2024. All rights reserved.