If weapon collided with the body deal damage, if weapon collided with other weapon do not deal damage.
Problem is that when two weapons collide it detects it as if Player object and Goblin object collide and not that actual weapons collide.
I have tried:
public class ObjectDamage : MonoBehaviour
{
public int damage;
public int team;
public float bounceForce = 5f;
void Start()
{
}
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log($"Collided with object with tag: {collision.gameObject.tag}");
if (collision.gameObject.layer == 3)
{
ClashWithOtherWeapon();
Debug.Log("Reached!");
return;
}
ObjectHealth objectHealth = collision.gameObject.GetComponent<ObjectHealth>();
if (objectHealth != null && objectHealth.team != team)
{
objectHealth.TakeDamage(damage);
BounceEnemyBack(collision);
}
}
private void ClashWithOtherWeapon()
{
return;
}
private void BounceEnemyBack(Collision2D collision)
{
Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 bounceDirection = collision.transform.position - transform.position;
bounceDirection.Normalize();
rb.AddForce(bounceDirection * bounceForce, ForceMode2D.Impulse);
}
}
}
You should check collision.collider
, which represents the Collider2D
component that collides with the current object.
And collision.gameObject
refers to the game object where the Rigidbody2D
associated with the collider is attached.