我正在做一个游戏,就像一个消防员,我已经制作了健康和氧气对象并在其上放置了2个脚本,下面是我的氧气和健康脚本代码。 我面临的问题是,当我将这个重复的物体放在任何其他位置时,就像在地图上有2个火,然后在第一个物体健康和氧气工作正常,但在第二个物体,健康栏工作正常,但氧吧不起作用精细。
这是氧气的脚本。
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; } }I am working on a game, like a fire fighter, I have made health and oxygen object and place 2 scripts on it, below is my code of oxygen and health scripts. I am facing problem that, when I place this duplicate object in any other position, like have 2 fires on map, then at first object health and oxygen works fine, but at 2nd object, health bar works fine but oxygen bar doesn't work fine.
here is the script of 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; } }Here is the script of 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; } }最满意答案
地狱呀,它解决了。 在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; }It sort out. In Update function, distance function conflicts with others so that instead of Distance, I have used OnTriggerStay and it works fine .
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; }更多推荐
发布评论