private void OnTriggerEnter2D(Collider2D collision) { IDamagee damagee = collision.gameObject.GetComponent <IDamagee>(); if (damagee != null) { ApplyDamage(damagee, collision); } }
private void OnTriggerStay2D(Collider2D collision) { int instanceId = collision.gameObject.GetInstanceID(); if (!attacked.ContainsKey(instanceId)) { IDamagee damagee = collision.gameObject.GetComponent <IDamagee>(); if (damagee != null) { attacked.Add(instanceId, tick); ApplyDamage(damagee, collision); } } }
protected virtual void ApplyDamage(IDamagee damagee, Collider2D collision) { if (damagee.GetAllyType() == damageSource || // friendly !damagee.IsVulnerableToDamage()) // immune to damage { return; } float deltaX = collision.transform.position.x - transform.position.x; Vector3 impulse = forceDirection * force; impulse.x *= (deltaX >= 0 ? 1 : -1); damagee.Damage(damageAmount); damagee.KnockBackFromDamage(impulse); }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Ground")) { Instantiate(instantiatedObject, transform.position, Quaternion.identity); Destroy(gameObject); return; } IDamagee damagee = collision.GetComponent <IDamagee>(); if (damagee == null || damagee.GetAllyType() == damageSource || // friendly !damagee.IsVulnerableToDamage()) // immune to damage { return; } Instantiate(instantiatedObject, transform.position, Quaternion.identity); Destroy(gameObject); }