# Deal Damage to Player

I have to make a simple "avalanche" game to show I know how to use the physics and creating terrain. But I actually wanted to go a little above and beyond and actually give the player health so there is a actual purpose to dodging the sphere objects. I found a very useful guide on how to set up player health, the only thing is I don't know how to set up the player taking damage when he collides with a sphere. Here is the code I have for the players health

#pragma strict

public var health : float = 100f;                           // How much health the player has left.
public var resetAfterDeathTime : float = 5f;                // How much time from the player dying to the level reseting.
public var deathClip : AudioClip;                           // The sound effect of the player dying.

private var anim : Animator;                                // Reference to the animator component.
private var playerMovement : PlayerMovement;            // Reference to the player movement script.
private var hash : HashIDs;                             // Reference to the HashIDs.
private var lastPlayerSighting : LastPlayerSighting;    // Reference to the LastPlayerSighting script.
private var timer : float;                                  // A timer for counting to the reset of the level once the player is dead.
private var playerDead : boolean;                           // A bool to show if the player is dead or not.

function Awake ()
{
// Setting up the references.
anim = GetComponent(Animator);
playerMovement = GetComponent(PlayerMovement);
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
}

function Update ()
{
// If health is less than or equal to 0...
if(health <= 0f)
{
// ... and if the player is not yet dead...
// ... call the PlayerDying function.
PlayerDying();
else
{
// Otherwise, if the player is dead, call the PlayerDead and LevelReset functions.
LevelReset();
}
}
}

function PlayerDying ()
{
// The player is now dead.

// Set the animator's dead parameter to true also.

// Play the dying sound effect at the player's location.
AudioSource.PlayClipAtPoint(deathClip, transform.position);
}

{
// If the player is in the dying state then reset the dead parameter.
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.dyingState)

// Disable the movement.
anim.SetFloat(hash.speedFloat, 0f);
playerMovement.enabled = false;

// Reset the player sighting to turn off the alarms.
lastPlayerSighting.position = lastPlayerSighting.resetPosition;

// Stop the footsteps playing.
audio.Stop();
}

function LevelReset ()
{
// Increment the timer.
timer += Time.deltaTime;

//If the timer is greater than or equal to the time before the level resets...
if(timer >= resetAfterDeathTime)
// ... reset the level.
}

public function TakeDamage (amount : float)
{
// Decrement the player's health by amount.
health -= amount;
}


Thanks for the help guys, and helping me to learn unity.

First off, if you are that new to Unity, maybe you would benefit a lot from going through tutorials. Unity website has some pretty handy tutorials, plus some nice step-by-step complete projects.

Nevertheless I´m going to give you a general answer to guide you with your question. For my answer I assume you are familiar with some basic Unity3D concepts, as you point out, in order to avoid overexplaining every little detail.

I guess the balls you mention have a Rigidbody in order to be affected by physics. These Rigidbody have a function called OnCollisionEnter, that is called when the Rigidbody hits another Rigidbody/Collider. If your players doesn´t have one of those, you will need to add a Collider in order to allow it to interact with the balls. CapsuleCollider is used usually for players.

Once you have both objects with a Rigidbody/Collider, you can write the OnCollisionEnter function on the ball. Notice that OnCollisionEnter takes as a parameter a Collision, which has a parameter that is the gameObject the ball has collided with. Then you will need to make sure that it is the player, and then call the TakeDamage function.

I hope that helps.

Check out colliders. You'd likely want to implement OnCollisionEnter in your character class. When something collides with the player, this method will be activated. Inside this method is where you'll deduct from the player's health.

If the player can only collide with one thing (the sphere objects), you can make it pretty simple:

function OnCollisionEnter(collision : Collision) {
TakeDamage(5);
}


Read up on what's required for the OnCollisionEnter method to be called, to ensure you have your scene set up correctly (i.e. colliders attached and rigid bodies where needed).