i am making my first unity 3d game, its an FPS game and i have been using tutorials on the internet for making it so far. however, i am finding it really difficult to code it so that either the player or an NPC can take damage. to loose health i would like the characters to have to have been shot or knifed as they are the main ways to take damage in my game. i would also like it if the characters could take fall damage as well.
i would be really great full if someone could tell me how, i would prefer it if it was in C# as thats the language i have programed in so far.
This is potentially quite complex, but I’ll provide some suggestions
:
Create a script for an Entity as a place to store the health of a player of entity - i.e. a Health property
Add this component to your player’s and enemies
When a player or enemy takes damage you can deduct health from this script’s Health property
Falling damage could be calculated by caching the entity’s location when they are no longer grounded and calculating the distance since when they collide with something - perhaps from a particular layer - i.e. a floor layer. see OnCollision
Entities stabbing could be another collision, but it’s likely you’ll want an enemy to detect whether it is close enough to stab and then you can determine whether the stab was successful - i.e. it’s always successful, or there may be light RPG properties for an entity such as armour/agility whatever.
Player’s being shot is tricky, but I suggest firing rays and animating your bullet geometry through the world, your bullet can collide with entities - and if it does then they take damage - but you will want to fire rays to see if the distance the bullet will travel from frame to frame would intersect an entiity.
or use simple laser type bullets that take no time to travel half a KM
Sorry it’s not a complete answer, but there are many ways to do what you’re asking and there’s quite a lot to each of the answers.