Hi, I am tring to make health to my player but I don’t know how…
I found this script:
var health : float = 100;
function ApplyDamage( amount : float ){
health -= amount;
if ( health <= 0 ) Dead();
}
function Dead() {
print ( name + " is dead now.");
}
But now I need to make my player loose healt like If player hits gameobject"enemy" loose 10 healt…Does anybody know how to do this???
As @kevork suggested, you can add this function at your script:
function OnCollisionEnter (theCollision : Collision) {
if (theCollision.gameObject.tag=="Enemy"){
ApplyDamage(10);
}
}
If your enemies have a tag "Enemy" assigned to them, the function above means: "if the player collides with an "Enemy", call ApplyDamage(10) (10 indicates the life that player will lose)" - what will follows is in the script that you have reported in the question!
Look at here for better explanations about onCollisionEnter: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
As the link reports, "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached".
If you are not full-practiced with Unity's interface yet, and you don't know how to attach a rigidbody to an object, look at here: http://unity3d.com/support/documentation/Components/class-Rigidbody.html.