Hey, i have a life system and i need to make a script that instantiate a 3d text with the health number that i have lost or gain, but i dont really know how to check if the player lost or gain health, that is the script: (its in C# but i use javaScript more, so C# or javaScript are both ok for me).
using UnityEngine;
using System.Collections;
public class playerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public GUISkin _theSkin = null;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI(){
if (_theSkin != null)
{
GUI.skin = _theSkin;
}
GUI.Box(new Rect(10,10, healthBarLength, 20), curHealth + "/" +maxHealth);
}
public void AddjustCurrentHealth(int adj){
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
curHealth = maxHealth;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
i have found it in a tutorial series, and i have made a font + a 3d text prefab with animations and everything but all i need now is the check when the player lose or gain health and instantiate the 3d text prefab with the number of the health loss or gained.
i wish it was that easy, im making an rpg style game and i have like 100 hp and everytime i get hit, i lose a random number between 5 - 20 hp so i need to make nubers that goes up the player and disapear saying how much hp i have loss in red and how much hp i have gained (from skills and etc) in blue.
thers a player script, if the player is hitting a fallout tagged mesh, it calls the curDamage (from the script of the hpbar) says: curdamage - Random.range(5,20);
Well, you simply call at the same time the function visually popping the damage on your character (instantiating a 3D Text, all that stuff), by giving it the value to display.
When you call Random.Range (x, y), it returns you a random value between x and y. If you call it again, you will get another value. So, you must temporarily store the first returned value, and work with it.
thanks that makes sense, ill try that, but i have another question, can i make it like this?
make an int var called x
and say
if i hit a fallout tagged object, x = random.range(5,20) and than curHealth = curHealth-x.
and reset x everytime i use it?
and instead of using curHealth, ill use x for instantiating.
does that makes sense?
Whenever you assign a value to x (like, x = Random.Range), you overwrite the previous value, so there is no real need to “reset” it (unless special circumstances).
Since x is the life lost, you will work with x, when you want to display the life lost.
alright!
one last question, is there a way or a function or something that check if the variable has changed? like from 13 to 5 to 3 to 2 so i could say, if that changes, instantiate a prefab with the text of the variable x.
thanks