I’m trying to make it so so when the enemy touches the player, the player gets hurt, and the enemy dies. But the player in this case is a plant (theres a player which is guarding plants from mosquitos, and if the plant gets hit, the enemy dies, and the plant gets damaged.) I dont quite understand 0nTriggerEnter2D, and I haven’t found any tutorials that helped. I don’t fully want you to write the code for me, just help me understand it better. Heres my code.
public float health = 2f;
void PlantDeath()
{
Destroy()
}
void OnTriggerEnter2D(Collider2D col)
{
// if collider is enemy, subtracts 1 hp
if (other.gameObject.tag == ("Enemy"))
{
health -= 1f;
// when hit, changes color
SpriteRenderer sr;
sr = gameObject.GetComponent<SpriteRenderer>() as SpriteRenderer;
sr.color = new Color(243, 134, 1);
other.GameObject.health -= 5000;
if (health <= 0)
{
PlantDeath()
}
}
}
}