Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Physics2D.BoxCast questions

Discussion in '2D' started by Falcon8857, Jun 30, 2022.

  1. Falcon8857

    Falcon8857

    Joined:
    Feb 14, 2016
    Posts:
    6
    Hi all,

    I'm trying to use the Physics2D.BoxCast function to check for a collision. I'm sure there I'm missing something because it only works if the left edge of the box casted collides with the right edge of the target collider. This is the function:

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.BoxCast(
    2.             this.boxCollider.bounds.center,
    3.             this.boxCollider.size,
    4.             0,
    5.             Vector2.left,
    6.             0.1f,
    7.             this.target
    8.         );
    The target is the layermask of an object with boxcollider2d. The collider is just a collider I assigned from the editor.

    Another thing, if I put 0 instead of 0.1f in the distance there is no collision at all. Any help with this please? What I want to achieve is check if the casted box collides with another collider, at any point, I mean, even if they are overlapping. Sure this is a very newbie mistake but I'm a newbie :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    You're in luck: this question sounds like it is right up @MelvMay 's alley!
     
    MelvMay likes this.
  3. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    You probably want to use OverlapBox instead of BoxCast with distance 0.
     
    Falcon8857 likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    As above, use OverlapBox if you only want an overlap check.

    Also, this looks like it's right out of one of those awful tutorials which use a collider then read and interpret its dimensions form its properties and then pass it into a query which is a lot of sillyness.

    If you look at all Colliders you'll see you can skip all that sillyness and just ask to cast that collider or check what it overlaps and other things too. This means you have to provide far fewer details which is less prone to error:

    Cast
    Overlap
     
    Falcon8857 likes this.
  5. Falcon8857

    Falcon8857

    Joined:
    Feb 14, 2016
    Posts:
    6
    Thank you guys OverlapBox works perfectly!

    I don't really know much about this, I'm just trying to follow the examples I see and then adapt them to my needs :( I'm using colliders with the OnTriggerEvent2D for other things but in this case is an enemy with his collider for the body and another one in front for the attack so when the player touches the attack collider I trigger the attack animation, bt the attack has some delay and the damage is triggered in a certain point during the animation so I need to let the player keep inside the collider and then check for overlap again when the animation triggers the damage function.

    Even if I use the OnTriggerEvent2D for starting the animation I need a way to check later if the player is still overlaping the collider or has dodge the attack. Is another better way of doing this?

    Edit: the first thing that comes to my mind is having an Attack flag and set it to true when the animation frame triggers the event, but I don't know if that's more effective... thoughts?
     
    Last edited: Jul 2, 2022