2016-05-22 3 views
0

Ich arbeite an einem Spiel, wie ein Feuerwehrmann, ich habe Gesundheit und Sauerstoff Objekt und legen Sie 2 Skripte drauf, unten ist mein Code für Sauerstoff und Gesundheit Skripte. Ich stehe vor dem Problem, dass, wenn ich dieses doppelte Objekt an einer anderen Position platziere, wie 2 Feuer auf der Karte haben, dann auf den ersten Objekt Gesundheit und Sauerstoff gut funktioniert, aber bei 2. Objekt funktioniert Gesundheitsbalken gut, aber Sauerstoffbar funktioniert nicht fein.Gesundheitsbalken reduziert, aber OXYGEN Bar ist nicht an verschiedenen Objekten

Hier ist das Skript von 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; 
} 
} 

Hier ist das Skript für Gesundheit

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

Dieses Englisch macht meinen Kopf verletzt. – 13aal

+0

@ 13aal können Sie bearbeiten – Sherlock

+0

@FarhanAli Ich bin nur mit dir Mann, entspannen, nicht demotiviert, weil jemand im Internet gemein zu dir ist. Es ist das Internet. Ich kann Arabisch und Englisch fließend sprechen, also keine Notwendigkeit, sich zu verdrehen. – 13aal

Antwort

0

Hell Yeah, es aussortieren. In Update-Funktion, Distanz-Funktion Konflikt mit anderen, so dass anstelle der Entfernung habe ich OnTriggerStay verwendet und es funktioniert gut.

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; 
} 
Verwandte Themen