Question by
noahameister · Jun 30, 2019 at 09:45 AM ·
gameenemydamagehealth
How to make my enemies damage player?
I am trying to get it so that on collision, enemies will deal damage to player. here is my player health script
public class playerhealth : MonoBehaviour {
public int health = 100;
public GameObject deathEffect;
public void PlayerDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
and this is my enemy script:
public class Enemy : MonoBehaviour { public int health = 100;
public int Enemydamage = 10;
public GameObject deathEffect;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.transform.tag == ("Player"))//Player
{
collision.GetComponent<playerhealth>().PlayerDamage(Enemydamage);
}
}
}
Help ples, player too invincible
Comment