0

I am using an overlap box to detect whether an arrow hits the ground or an enemy, the arrow hitting the enemy works, it's that often the arrow will just pass through the ground, not sure if I am missing something.

Heres my code for detecting the ground or enemy.

private void FixedUpdate()
{
    damageHit = Physics2D.OverlapBox(damagePosition.position, damageSize, whatIsEnemy);
    groundHit = Physics2D.OverlapBox(damagePosition.position, damageSize, whatIsGround);

    if (!hasHitGround)
        {

              if (damageHit)
            {
                Collider2D[] detectedObjects = Physics2D.OverlapBoxAll(damagePosition.position, damageSize, whatIsEnemy);

                  foreach (Collider2D collider in detectedObjects)
                {
                          IDamageable damageable = collider.GetComponent<IDamageable>();

                          if (damageable != null)
                    {
                        damageable.Damage(weaponData.attackDamage);
                        Destroy(gameObject);
                    }

                    IKnockbackable knockbackable = collider.GetComponent<IKnockbackable>();

                    if (knockbackable != null)
                    {
                        knockbackable.KnockBack(weaponData.knockbackAngle, weaponData.knockbackStrength, (int)Mathf.Sign(transform.rotation.y));
                    }
                }
            }

          if (groundHit)
            {
                rb.gravityScale = 0f;
                rb.velocity = Vector2.zero;
                hasHitGround = true;
                Destroy(gameObject, 10f);
            }
            else if (Mathf.Abs(xStartPos - transform.position.x) >= travelDistance && !isGravityOn)
            {
                isGravityOn = true;
                rb.gravityScale = gravity;


        }

        }

}

private void OnDrawGizmos()
{
    Gizmos.DrawWireCube(damagePosition.position, damageSize) ;
}

I thought that the hitbox may have been to small and it would just pass through wall due to not hitting it on any frame. After making the hitbox larger it still did not help, here you can see that even though the overlap box is overlaping the collider the arrow will continue on.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Jukeo
  • 1
  • not related t your issue just a tip: instead of the `!= null` checks you can directly use `if(collider.TryGetComponent(out var damagable)){ ...}` .. makes it a little bit cleaner and easier to read ;) – derHugo Nov 16 '22 at 10:18
  • Why don't you use e.g. `OnCollisionEnter2D`? – derHugo Nov 16 '22 at 10:21
  • @derHugo yeah cheers, i fixed by switching over to a trigger collider which turns out is much more reliable. – Jukeo Nov 16 '22 at 17:41

0 Answers0