Health system final

Okay I’ve decided I’m restarting on my health system. I want it to work the same but built where it will actually work for my game. I need more advice on how to build it. The last health system shattered to pieces but the simple idea still stands. If you haven’t seen the old health system their no need to go back I will describe it again.

THE IDEA

  • Okay the idea start with a health variable, starting you out with 100 health.
    If health equals less than 0 then you are dead.
  • If you are hit by a gameObject (also rigidbody) with the tag Arrow. then subtract 10 points from your health.
  • If you are dead (player) transform to (0,0,0).
  • If enemy is dead (AI) destroy gameObject.

I have no idea what went wrong with the last one so don’t even look at it. For the player I have a Character controller. For the AI I have a Rigidbody (for now)

I really need this script, I’m at the point where I really need it.

Basically, to have a collision message sent, you need a collider to interact with an other collider because of a physic impulse. Most of the time, it will be a force applied on a rigidbody that makes on collider enter another.

Then, you need a script attached to one of the collider that implement the function OnCollisionXXX or OnTriggerXXX. In your case, you have to decide if the arrow get the message then apply damage on the character OR if the character get the message and receive the damage. No big differences here, just need to be clear for you.

If at least one of the two collider is a trigger, the message sent will only be OnTriggerXXX. To understand what messages are sent, check this example.

Note that collision can be a problem when an object travel too fast (typically a bullet). An arrow should be ok though.