void FixedUpdate() { Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, radius, layerMaskValue); foreach (Collider2D collider in hits) { EntityWithHealth e = collider.gameObject.GetComponent <EntityWithHealth>(); if (e != null) { bool entityDied = e.LoseHealth(damage); if (entityDied) { if (e.gameObject.tag == "Badman") { InventoryManager.main.GainMana(4); } else { InventoryManager.main.GainMana(2); } } } } Destroy(gameObject); }
private void OnCollisionEnter2D(Collision2D collision2D) { int collisionLayer = collision2D.gameObject.layer; if (Tools.IsInLayerMask(collisionLayer, config.TargetLayers)) { EntityWithHealth targetEntity = collision2D.gameObject.GetComponent <EntityWithHealth>(); if (targetEntity != null) { isInContact = true; StartDealingDamage(targetEntity); //Debug.Log("<color=green>In contact with " + collision2D.gameObject.name + "</color>"); } } }
private void OnCollisionExit2D(Collision2D collision2D) { EntityWithHealth targetEntity = collision2D.gameObject.GetComponent <EntityWithHealth>(); if (targetEntity != null && targetEntity == target) { //Debug.Log("<color=yellow>Losing contact with " + collision2D.gameObject.name + "!</color>"); isInContact = false; target = null; if (anim != null) { anim.Play("Walk", -1, 0); anim.SetBool("Smashing", false); } } }
void OnCollisionEnter2D(Collision2D collision2D) { if (config.HitSound != null) { AudioSource.PlayClipAtPoint(config.HitSound, Camera.main.transform.position); } if (config.ShouldBounce && collision2D.gameObject.layer == BOUNCE_LAYER) { if (velocityBeforeCollision.normalized.y < -0.2f) { rb.velocity = new Vector2(velocityBeforeCollision.x, -velocityBeforeCollision.y); } else { var velocity = new Vector2(-5 + Random.value * 10, 10 + Random.value * 5); rb.velocity = velocity.normalized * velocityBeforeCollision.magnitude; } collider.enabled = false; collisionTimer = Time.time + 0.2f; return; } EntityWithHealth e = collision2D.gameObject.GetComponent <EntityWithHealth>(); if (firstHit && e != null) { firstHit = false; bool entityDied = e.LoseHealth(config.Damage); if (entityDied) { if (e.gameObject.tag == "Badman") { InventoryManager.main.GainMana(3); } else { InventoryManager.main.GainMana(1); } } } Kill(); }
private void StopDealingDamage() { target = null; }
private void StartDealingDamage(EntityWithHealth targetEntity) { target = targetEntity; ResetDamageInterval(); damageTimer = 0f; }
private void Start( ) { boxCollider2D = GetComponent <BoxCollider2D>(); entityHealth = GetComponent <EntityWithHealth>(); }