健康栏正在减少,但OXYGEN栏不在不同的对象(Health bar is reducing but OXYGEN bar isn't at different objects)

编程入门 行业动态 更新时间:2024-10-21 23:02:44
健康栏正在减少,但OXYGEN栏不在不同的对象(Health bar is reducing but OXYGEN bar isn't at different objects)

我正在做一个游戏,就像一个消防员,我已经制作了健康和氧气对象并在其上放置了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; }

更多推荐

本文发布于:2023-08-05 05:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   健康   OXYGEN   Health   reducing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!