Help with script for losing health

So I have a very simple AI on this game I’m working on. basically when you pass an enemy it starts following you, now I was wondering if anybody could please help me create a second script to go along with this, basically i want the player to lose “health” when the enemy collides with him, it seems so simple but i have been having nothing but challenges with this. Thank you to anybody that can help me :slight_smile:

I would make sure your enemies are Tagged as “Enemy” and then have your player collider be a trigger and use OnTriggerEnter() to activate your player’s LoseHealth() function when triggered by anything tagged as “Enemy.”

Post the code you have been having problems with.

As is often noted, the characterController does not detect collisions unless it is during a Move operation, so if the player is standing still it will not detect that it is being hit by a enemy (rigidbody-driven AI, projectile, etc.)

Another approach can be to have each enemy do its own OnCollisionEnter and then check if the collider is with a tag of “Player”, and if so, then do a SendMessage to the player to Lose Health. This way you could also have different kinds of enemies in which one might say SendMessage(“LoseHealth”, 10) and another SendMessage(“LoseHealth”, 20 ) - thereby storing the damage level in each enemy rather than having to store the variations in the player script.

If you want the Player to be able to detect collisions, including when standing still, then there is the option of Adding a second collider to the Player and scaling it a bit larger than the characterController capsule, but the above option may be more straightforward.