- Home /
Health bar isn't decreasing.
This doesn't seem to decrease on every collide. Yes I've set the sword to have a box collider. But nothing seems to work.
@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;
private static var health : int = 100;
function OnGUI () {
GUI.DrawTexture(Rect(0,Screen.height - 600,400,100), healthBorder);
//Now on top we can put Health Bar texture
var adjust : int = health * 3; //adjusting texture size (width) / health(100)
GUI.BeginGroup(Rect(135,Screen.height - 565,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}
/**
Function to handle collision
*/
function OnTriggerEnter(other:Collider)
{
health -= 10;
}
Put a debug statement in your onTriggerEnter and validate that the trigger is being called.
Wouldn't it be easier to drop the 'Group' and change the size of the texture rect?
Note - you may have to add code to deal with different aspect ratios if you want the health texture image to not stretch on other window/layout systems.
@kolban it is being validated.
@IgnoranceIsBliss - At this stage it isn't much of a worry for me as I'd prefer to get the game functionality working before I focus on the design.
Ahh, are you using BeginGroup and EndGroup to as a way to clip your texture so it doesn't visibly contract as the health bar gets shorter.
In your OnTriggerEnter, after you subtract from the health amount, use Debug.Log to actually let you know the health amount. Same for your OnGUI function.
You may find it creates a LOT of messages in your console, but it will show for a fact that it is an issue with your data rather than an issue with your drawing code.
Answer by FizzyBear · Apr 19, 2012 at 04:46 AM
Fixed this by calling the component of the health variable.
function OnTriggerEnter (d:Collider)
{
print ("DIE");
if (animation.Play("attack")== true) {
GetComponent(Motor).health -= Random.Range(0,10);
}
}
Answer by darkcookie · Apr 16, 2012 at 04:57 AM
if i was you i would do it like this .......
c#...
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public GameObject Drop;
public GameObject RagDroll;
public float healthBarLength;
public GUIStyle myStyle;
// Update is called once per frame
void Start ()
{
healthBarLength = Screen.width / 3;
}
void Update ()
{
AddjustCurrentHealth(0);
}
void OnGUI ()
{
GUI.Box(new Rect(10, 32, healthBarLength, 20), curHealth + "/" + maxHealth, myStyle);
}
public void AddjustCurrentHealth(int adj)
{
curHealth += adj;
healthBarLength = (Screen.width / 3) * (curHealth / (float)maxHealth);
if(curHealth < 0)
{
Die ();
}
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
}
public void Die ()
{
Destroy(gameObject);
Instantiate(Drop, transform.position, transform.rotation);
Instantiate(RagDroll, transform.position, transform.rotation);
}
}
now if u want is to take damage from an enemy ....i would do something like this.....
using UnityEngine; using System.Collections;
public class EnemyAttack : $$anonymous$$onoBehaviour { public GameObject target; public int damage = 10; public float attackTimer; public float coolDown;
// Use this for initialization
void Start ()
{
attackTimer = 0;
coolDown = 7.0f;
}
// Update is called once per frame
void Update ()
{
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0)
{
Attack();
attackTimer = coolDown;
}
}
// attack
void Attack ()
{
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 6f)
{
if(direction > 0)
{
PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
ph.AddjustCurrentHealth(-damage);
animation.CrossFade("attack",0.0f);
animation.Stop("idle");
animation.Stop("run");
}
}
}
}
Getting null reference on this line:
ph.AddjustCurrentHealth(-damage);
How do I fix that?
That would mean you don't have a 'PlayerHealth' component on the object that is taking damage (your player).
$$anonymous$$ake sure you've selected your player object and chosen 'PlayerHealth' from the Components|Scripts menu.
In order to CHANGE your player health, the script has to first get access to it. The PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth"); line does this by asking for the 'PlayerHealth' script from the target object.
Ah yeah, I just fixed that. But now I'm getting
It's still not removing the 10 HP from the health :/
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
collider animation 1 Answer
Unity 4.5.2 bug changing the properties of the 2d colliders in animation 1 Answer
Triggering door animation with collider 2 Answers