Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use the new Physics2D.OverlapCapsule & CapsuleCast

Discussion in '2D' started by RandomDood, Feb 7, 2017.

  1. RandomDood

    RandomDood

    Joined:
    Nov 14, 2013
    Posts:
    7
    So I can't find anything on this so I guess I will be the first one to ask.

    I could use some help understandinng how to use the new Physics2D.OverlapCapsule, Physics2D.CapsuleCast, and the similar 2D Capsule additions made.

    Basically I need to get all 2Dcolliders that are in a weirdly shaped area and casting with the capsule seems to be the best way. I have messed with it but don't have a understanding of how all the fields work, what they do and the documentation is a little confusing for me.

    Also is there a way to visualize the capsule in the scene view for debug and adjustment purposes?

    Thanks in advance.
     
    frenchfries5 likes this.
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The parameters for the 2D capsulecast are not consistent with the 3D capsulecast for some reason.

    You can think of a 2D capsule as two circles connected by a box. You can visualize it that way using Gizmos or Debug shapes, or use a capsule collider.

    I've never actually used these methods, but this is my understanding:
    • Origin is the centerpoint of the capsule.
    • Size represents the width and height of the capsule
    • CapsuleDirection is either CapsuleDirection2D.Vertical or CapsuleDirection2D.Horizontal (this is which way the capsule is oriented, meaning which sides are the rounded ones). Vertical means the circles are on top and bottom, with Horizontal the circles are on the left and right.
    • Angle is the Z rotation of the shape.
    • Direction is which way to sweep the shape.
    • Distance is how far to go.
     
    Last edited: Feb 7, 2017
  3. RandomDood

    RandomDood

    Joined:
    Nov 14, 2013
    Posts:
    7
    OK, yeah I think I got it now.

    However for some reason when I use physics2d.OverlapCapsuleAll, it seems to not pick up or ignore all the 2d colliders below the centerpoint. Could that be a bug?

    Vector2 pos = position;
    Vector2 size = new Vector2 (1f, 1.1f);

    List<Collider2D> collection=
    (Physics2D.OverlapCapsuleAll( pos, size, CapsuleDirection2D.Vertical, 0, 1<<LayerMask.NameToLayer("Enemy"))).ToList();
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You know, I'm not really sure about that one. Does the same thing happen with a Horizontal capsule?

    But you can skip the bitshifting layermask if you wanted by making a LayerMask type variable, which can be assigned in the inspector with a drop down menu and passed to the function.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    Not sure what version you're using but there were a number of physics fixes applied, some for the capsule intersection tests so maybe try the patch releases for your version.
     
    LiterallyJeff likes this.
  6. RandomDood

    RandomDood

    Joined:
    Nov 14, 2013
    Posts:
    7
    Thanks for the replies.

    I'm using version 5.5 1f1

    Have been playing around with it and I found a fix though its kind of weird. From what I observed its like the 2D capsule for overlap is using almost the same logic as the 3D capsule. What I mean by that is that it use 2 circles for the top and bottom of the capsule and and fills the space in between, although I know that's how properties make it to be. I only came to that conclusion by taking jeffreyschoch advice and used a non intractable capsule collider to visualize the positioning when I ran the overlap check and things didn't line up properly.

    I had to offset the vertical position downwards because it seem that the Y position was centering on the "bottom circle" of the capsule

    Vector2 size = new Vector2 (1f, 1.4f);
    Vector2 pos = new Vector2( position.x, position.y - 0.2f); // offset position down wards

    List<Collider2D> collection=
    (Physics2D.OverlapCapsuleAll( pos, size, CapsuleDirection2D.Vertical, 0, 1<<LayerMask.NameToLayer("Enemy"))).ToList();
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    Try looking at the manual for Capsule Collider 2D. It clearly shows that the 'size' property is an encapsulating box (OOBB). The width/height of the box is the full width/height of the capsule which removes the need to specify a radius explicitly i.e. you just set the boxed area you want the capsule to cover and the orientation and the rest is done for you.

    With an orientation of 'CapsuleDirection2D.Vertical', this would produce a capsule with a width of 1 and height of 1.4. Doesn't get much easier than that. The half-height of the capsule is (1.4 / 2) = 0.7 so assuming no rotation, the bottom of the capsule is 0.7 below the position and the top of the capsule is 0.7 above the position. Also, the radius would be calculated as half the width so 0.5.

    This means that the position of the capsule would need to be 0.7 above any surface.


    I implemented a bunch of fixes that included the CapsuleCollider2D in patch release 5.5.1p1. That said, they were more performance fixes and edge-cases where the overlaps were not being solved.
     
  8. RandomDood

    RandomDood

    Joined:
    Nov 14, 2013
    Posts:
    7
    Okay. Thanks for the help :). I have things working now so it isn't a big deal, but I will make a clean project to make sure my math isn't messed up somewhere. Like I said it was mainly only the overlap variation of the new capsule features, but I don't see a reason why they wouldn't use the same logic.

    If I get the same issue then I'll send you the file for you to look at.
     
    MelvMay likes this.
  9. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    Hello,
    I'm trying to use OverallCapsuleAll but I can't get it to work.
    I have the center right, the size right (I think, not being able to visualize it makes it harder but it should be good), but I am not sure about the angle. What does it need? a Euler Angle? between 0 to 360? 180 to -180 ok? Radiant?
    Thanks for the help!

    f.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    The Capsule is defined in the same way as a box. Imagine defining a box surrounding the capsule so that you're defining the size of that which equates to the width and height of the capsule. Best way to visualize this in your mind is to add a CapsuleCollider2D and change its size property. Set it to 1 width and the capsule will be 1 wide. Set it to 3 height and the capsule will be 3 in height. This is identical to a BoxCollider2D and doesn't require imagining sizes of end-cap radius etc for the capsule so should be much simpler.

    The angle is the same as all other angles in 2D physics; you specify them as a single scalar value in degrees and can be whatever value you like and is not limited to a specific range.
     
    LiterallyJeff likes this.
  11. as3mbus

    as3mbus

    Joined:
    Dec 5, 2016
    Posts:
    71
    i somehow still doesnt understand how this capsule are drawn. i understand how they'll going to shape but idk where exactly they are going to be placed.
    should i give -y for size if i want to make the capsule below the first coordinate or it's always applied as positive so first coordinate always at bottom left corner of the box ?
    im using unity 2017.3 rn
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    The capsule is centered on the position exactly the same as the CapsuleCollider2D is. All the queries match the respective colliders in this regard so if in doubt, look at the properties on the respective collider. This is the same for all shape casts be it box, circle, capsule etc.
     
  13. as3mbus

    as3mbus

    Joined:
    Dec 5, 2016
    Posts:
    71
    ah i see now. i thought what you mean by drawing a box is defining 2 point like top left and bottom right and the capsule was inside the box. never know that the capsule canter are the coordinate itself.
    it's basically like the image below right ? for vertical capsule direction and 0 angle
    upload_2018-9-27_11-51-8.png
    thanks again
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    Yes, the image above is correct. As I said above, it's the same as how you define a CapsuleCollider2D which you can also visualize the same way.

    I'm wondering if it might be worth adding in some 2D gizmos that are available to use for drawing these queries.
     
    Last edited: Sep 27, 2018
    Krull likes this.
  15. as3mbus

    as3mbus

    Joined:
    Dec 5, 2016
    Posts:
    71
    idk about this 2D gizmos, but giving additional information that represent about how the capsule are made in api docs should help.
    I find it's really hard to understand parameter from description that provided in docs. including capsule direction and angle.
     
    DevBaro likes this.
  16. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi everyone,

    I read all your replies and still need help. I've been teaching myself how to code C# and use Unity for almost a year but, there's still a lot I don't understand and I ask for your patience.

    I'm trying to modify the free controller I got from a Youtube video so that it will use a capsule collider for the player. I replaced the "Physics2D.OverlapCircle" code with "Physics2D.OverlapCapsule" and tried using these variables below (based on the Unity manual and the replies in this thread):

    Vector2 capsuleCenter;
    Vector2 capsuleSize;
    CapsuleDirection2D capsuleDirection2D;
    float capsuleAngle;
    ...
    capsuleCenter = capsuleCollider.offset;
    capsuleSize = capsuleCollider.size;
    capsuleDirection2D = CapsuleDirection2D.Vertical;
    capsuleAngle = 0;
    ...
    Physics2D.OverlapCapsule(capsuleCenter, capsuleSize, capsuleDirection2D, capsuleAngle, m_WhatIsGround)

    For reasons unknown to me, there doesn't seem to be any collision detection.

    Additional notes/context: I'm trying to code in OverlapCapsule to detect a ceiling above the player when he is crouching and if there is a ceiling, further code will tell the player to remain crouched under an obstacle

    I look forward to your replies
    Kyle
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,327
    You're using the arguments correctly but the "capsuleCenter = capsuleCollider.offset" isn't correct. That's exactly that, an offset so if you don't set it, it'll be zero. The capsule center needs to be its world-space position so "transform.position + capsuleCollider.offset". Also, ensure that "m_WhatIsGround" is set correctly otherwise it'll filter out collisions you might want. It needs to be a bit-mask. Easiest way is to temporarily remove it or make sure it's of type LasyerMask, make it pubilc and set it in the inspector.

    If however you have an existing collider then you can just do Physics2D.OverlapCollider
     
    kylebengzon likes this.
  18. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi Melv,

    Thanks for the different options!

    1)
    I went ahead and applied your advice to the capsuleCenter and it looks like so:

    capsuleCenter = capsuleCollider.transform.position + (Vector3)capsuleCollider.offset;
    or
    capsuleCenter = (Vector2)capsuleCollider.transform.position + capsuleCollider.offset;

    I was getting the error "error CS0034: Operator '+' is ambiguous on operands of type 'Vector3' and 'Vector2'", which is why I had to convert(?) one into a Vector3 or 2
    Sadly, my character's capsule still doesn't detect any overlaps/collisions with the ceiling and goes right though it

    2)
    I then tried to use Physics2DOverlapCollider:

    ContactFilter2D contactFilter;
    Collider2D[] results;
    ...
    private void Start()
    {
    contactFilter.SetLayerMask(m_WhatIsGround);
    contactFilter.useLayerMask = true;
    }
    ...
    if(Physics2D.OverlapCollider(capsuleCollider, contactFilter, results))
    {
    crouch = true
    }

    But the if statement is giving the error "Cannot implicitly convert type 'int' to 'bool' "
     
  19. kylebengzon

    kylebengzon

    Joined:
    Sep 26, 2019
    Posts:
    11
    Hi Melv,

    I was able to get the OverlapCapsule to execute. I had to put a copy of the capsule variables (Center, Size, etc) into the FixedUpdate. It's still not keeping my player crouched under a platform, but that's a different block of code I'm working on. Thank you for all the help!
     
    MelvMay likes this.