I'm fairly new to unity and have encountered a issue to where when one of my game objects collide with the other then they both take damage. I have the damage taking down, but the issue comes when i ram into the opponent it takes damage but i as well take damage. Trying to wrap my head around how to fix so that he only takes damage from my impact and if the other game object runs into me then i take damage only and not him.
public void TakeDamage(float amount)
{
health -= amount;
healthbar.fillAmount = health / starthealth;
if (health <= 0)
{
gameObject.SetActive(false);
}
}
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "player")
{
collision.gameObject.GetComponent<playerScript> ().TakeDamage (damage);
}
}
This is what the take damage and collision looks like, the enemy collides with me and it uses the take damage method from the player script