Collider2D vs CapsuleCollider2D

Hello there!

In this lecture, Rick gave us the challenge of disabling jumping in mid-air. In the challenge slide he says we need to use:

Collider2D.IsTouchingLayers
LayerMask.GetMask(“layer”)

But after that he shows us the solution that uses CapsuleCollider2D, and not Collider2D. Does this make a difference, or does Collider2D just find whatever collider it finds on the game object? I used Collider2D in mine as instructed and it seems to be working. I guess it would be better practice to use the same type of collider regardless, just curious about the functionality.

Hi Semicolonist,

Welcome to our community! :slight_smile:

CapsuleCollider2D inherits from Collider2D, so a CapsuleCollider2D is also of type Collider2D. The IsTouchingLayers method is inherited from the Collider2D class, so it does not matter which method you call if you have a capsule collider 2D.

Long story short, your solution is perfectly fine.


See also:

*GeeksforGeeks: C# | Inheritance

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms