Tuesday, 14 May 2013
Health Damage and Health Bar Code
public class HealthBar : MonoBehaviour
{
// player's health and health bar
public float curHP=100;
public float maxHP=100;
public float maxBAR=100;
public float HealthBarLength;
void OnGUI()
{
// creates the health bar at the coordinates 10,10
GUI.Box(new Rect(10,10,HealthBarLength,25), "");
// determines the length of the health bar
HealthBarLength=curHP*maxBAR/maxHP;
}
void ChangeHP(float Change)
{
// takes whatever value is passed to this function and add it to curHP.
curHP+=Change;
// This if statement ensures we don't go over the max health
if(curHP>maxHP)
{
curHP=100;
}
// This if statement is to check if the player has died
if(curHP<=0)
{
// Die
Debug.Log("Player has died!");
}
}
// This function checks if the player has entered a trigger
void OnTriggerEnter(Collider other)
{
// The switch statement checks what tag the other gameobject is, and reacts accordingly.
switch(other.gameObject.tag)
{
case "Heal":
ChangeHP(5);
break;
case "Damage":
ChangeHP(-25);
break;
}
// Finally, this line destroys the gameObject the player collided with.
Destroy(other.gameObject);
}
}
Now we just need to add a timer which Alice is currently looking into :)
using UnityEngine;
using System.Collections;
public class HealthBar : MonoBehaviour
{
// player's health and health bar
public float curHP=100;
public float maxHP=100;
public float maxBAR=100;
public float HealthBarLength;
private float updateDisplayTime = 0F;
void Start()
{
updateDisplayTime = Time.time + 5F;
}
void Update()
{
if ( updateDisplayTime < Time.time )
{
curHP -= 3;
updateDisplayTime = Time.time + 5F;
}
if ( curHP <= 0 )
{
this.enabled = false;
}
}
void OnGUI()
{
// creates the health bar at the coordinates 10,10
GUI.Box(new Rect(10,10,HealthBarLength,25), "");
// determines the length of the health bar
HealthBarLength=curHP*maxBAR/maxHP;
}
void ChangeHP(float Change)
{
// takes whatever value is passed to this function and add it to curHP.
curHP+=Change;
// This if statement ensures we don't go over the max health
if(curHP>maxHP)
{
curHP=100;
}
// This if statement is to check if the player has died
if(curHP<=0)
{
// Die
Debug.Log("Player has died!");
}
}
// This function checks if the player has entered a trigger
void OnTriggerEnter(Collider other)
{
// The switch statement checks what tag the other gameobject is, and reacts accordingly.
switch(other.gameObject.tag)
{
case "Heal":
ChangeHP(5);
break;
case "Damage":
ChangeHP(-1);
break;
if ( updateDisplayTime < Time.time )
{
curHP -= 10;
updateDisplayTime = Time.time + 5F;
}
if ( curHP <= 0 )
{
this.enabled = false;
}
}
// Finally, this line destroys the gameObject the player collided with.
Destroy(other.gameObject);
}
}
The "private float updateDisplayTime = 0F;" we managed to find from one of Alisters lessons, so that there is a counter to how quickly the health decreases when you're near the trees.
Labels:
Alice Posts,
Cat Posts,
Coding
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment