A collision not detecting at high speed

Hello, I have an issue in my 2D game. I implemented a ledge grab which works with 2 collisions : one to detect the ledge to grab, and another one which verify that there is no “Ledge” layer above (so the player will grab the ledge at it’s top). When I jump it’s working pretty well, however if my character is too fast (-20 y velocity is the max), the box starts bugging and will sometimes detect the ledge and sometimes not. Does someone know how to fix this ?

A video that shows the problem : wfhc7k

We would need to know what the problem is to fix it. There’s no relevant information provided such as how you’re detecting the ledge and details etc.

Maybe you’re using Discrete rather than Continuous collision detection. Cannot see the Rigidbody2D component.

Yeah my bad.

So I have 2 local variables in my update() function that check :

  • if a ledge is in the collider : bool ledgeInCollision = Physics2D.OverlapBox(ledgeDetectionIn.position, new Vector2(0.08f, 0.05f), 0, ledgeLayers);
  • and another one to check if there is not any ledge above the 1st collider : bool ledgeOutNotCollision = !Physics2D.OverlapBox(ledgeDetectionOut.position, new Vector2(0.08f, 0.05f), 0, ledgeLayers);

Then I have my condition to define wheather or not the character should be considered as “onLedge” (also in the update() function): if (ledgeGrabCooldownTimer < 0 && ledgeInCollision && ledgeOutNotCollision && (isFalling || isOnLedge))

I had similar issues of my player passing through the ground and ignoring the collision at high speed and to fix it I changed in the rigidbody2D the collision detection to continuous. However I really don’t see how to increase the amount of “checks per seconds” for this collider.

So you’re using queries to detect the ledges? You shouldn’t be surprised that the body is at one position with no ledge to the right then it moves to the next position at speed and is below the ledge and there’s no ledge to the right. This is a problem with your way of detecting ledges.

The queries above are nothing to do with colliders, they’re physics queries. You’re asking of a box shape (not a collider) is overlapping something.

You cannot. The highest frequency is the frame-rate.

In the case above, continuous has nothing to do with it then because you’re not asking the physics engine to detect contacts, you’re performing queries.

It seems that this “box” you’re querying with needs to be much larger (it’s a tiny 8cm x 5cm box now). It seems it would make much more sense to cover more of the player and extend it vertically, potentially by the vertical velocity too. It’s better to ensure you detect them then discount them later with some other logic.

Okkkk, I was absolutely wrong. So if I understood well, to fix this issue, I would have to extend the vertical size of the box depending on my character’s velocity so even if my character is fast, the box will detect the ledge ?

Edit: I tried to simply increase the height of the boxes and It now works perfectly. Thanks for your fast and helpfull response ! (I’ll try to learn more about physic in unity because apparently everything is mingled in my head)

Yes, exactly, potentially adapting its vertical size so that in the next simulation step, they’ll overlap. “velocity.y * Time.fixedDeltaTime” is the vertical velocity per simulation step.

Yes, fixing a larger size will fix it up to a certain speed. Just be aware that if you move faster, it might break again.

Glad you got it working, good luck.

2 Likes