Need a script to attach to my player where every collision occurs with the player-object its heath decreases by 1 out of 10 and when it reaches 0 the player is destroyed(i.e. dies). I’m having a problem with OnCollision method so a small workable script would be enough.
Thank You.
using UnityEngine;
public class Player : MonoBehaviour {
public float health = 10f;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.gameObject.tag == "SetTagOfObjectHereAndInEditor")
{
health -= 1f;
}
}
void Update() {
if (health <= 0f)
{
// Player is dead. Delete him here etc...
}
}
}
Set The Player collider to trigger.
Add box collider to enemy
This will kill the player when trigger with the enemy