健康栏正在减少,但 OXYGEN 栏不在不同的对象

Health bar is reducing but OXYGEN bar isn't at different objects

我正在做一个游戏,就像一个消防员,我制作了健康和氧气对象并在上面放置了 2 个脚本,下面是我的氧气和健康脚本代码。我面临的问题是,当我将这个重复的对象放在任何其他位置时,比如在地图上有 2 次火灾,然后首先对象健康和氧气工作正常,但在第二个对象,健康栏工作正常但氧吧不工作很好。

这里是Oxygen的脚本。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class OXYGEN : MonoBehaviour {
public Transform Player;
public Transform OxygenBar;
public Transform TextIndicator;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
// method to check that player is range of fire flames or not
void Update () {
    float dist = Vector3.Distance(Player.position, transform.position);
    if(dist<3 )
    {
        reducetheoxygen();
    }
    if (dist > 3) {
        increaseOxygen ();
    }
}
//method to decrese the oxygen
void reducetheoxygen () {

    if (currentAmount > 0) {
        currentAmount-= speed *Time.deltaTime;
        TextIndicator.GetComponent<Text>().text=((int)currentAmount).ToString()+" %";
        } 
    else {
        TextIndicator.GetComponent<Text>().text="Oxygen End!!!!";
        Application.LoadLevel (4);
    }
    OxygenBar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}

//method to increase the oxygen
void increaseOxygen () {

    if (currentAmount == 100 || currentAmount >100) {
    }
    else{
        currentAmount+= speed *Time.deltaTime;
        TextIndicator.GetComponent<Text>().text=((int)currentAmount).ToString()+" %";
    }
    OxygenBar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}

这里是健康的脚本

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

public class health : MonoBehaviour {

// these are variables
public Transform Player;
public Transform HealthBar;
public Transform TextIndicator;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;

// method for checking distance that player in range of fire flames
void Update () {
    float distance = Vector3.Distance(Player.position, transform.position);

    if(distance<2 )
    {
        decreaseHealth();
    }
}
//method for decresing health
void decreaseHealth () {

    if (currentAmount > 0) {
        currentAmount -= speed;
        TextIndicator.GetComponent<Text>().text=((int)currentAmount).ToString()+"%";
    } else {

        TextIndicator.GetComponent<Text>().text="You are Dead!!!!!";
        Application.LoadLevel (4);
    }
    HealthBar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}

整理好了。 在 Update 函数中,distance 函数与其他函数冲突,因此我使用 OnTriggerStay 而不是 Distance,它工作正常。

 void OnTriggerStay(Collider obj)
 {

    if (obj.gameObject.tag == "coin"    ||obj.gameObject.tag=="coin1"||obj.gameObject.tag=="coin2") {
        Debug.Log ("colide");
        collided = true;
}
}
void OnTriggerExit()
{

    collided = false;
}