我正在开发一款游戏,比如消防员,我制作了健康和氧气对象,并在其上放置了两个脚本,下面是我的氧气和健康脚本代码。我面临的问题是,当我将这个重复的物体放在任何其他位置时,就像在地图上有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;
}
}
这是Health的脚本
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;
}
}
答案 0 :(得分:0)
天啊,是的,它解决了。 在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;
}