I'm building a simple 2D character controller based on raycasts. At this moment I'm trying to finish the player-ground interaction. For the ground I will be using edge colliders and no tiling information.

I have most code working but there's something I don't really know how to fix. Here is an screenshot explaining the situation:

enter image description here

As you can see, Player (purple rectangle) is on a slope. When player wants to move right the new x and y coords are calculated to set the player feet again on the slope (Blue rectangle). But, after this response has been applied to the player to make it out of the slope, It is pushed through the roof and some part of it is sticking out the edge collider (red box).

How do you handle this situation?

On the other hand, If there's any way to accomplish this in a simpler fashion I'm all ears :).


  • \$\begingroup\$ Have you considered using a boxcast instead of a raycast, to account for the full extents of your object? \$\endgroup\$ – DMGregory Mar 21 at 16:17
  • \$\begingroup\$ @DMGregory Yes, I have been trying with it but the problem I found is that boxcast can give contact points "inside" the edge colldier. I was trying to use boxcast + physics.Distance without luck. Was I missing something? \$\endgroup\$ – Notbad Mar 21 at 16:37
  • \$\begingroup\$ Can you demonstrate this problem for us with a minimal complete verifiable example? Seeing the code will help us suggest what to fix. \$\endgroup\$ – DMGregory Mar 21 at 16:43
  • \$\begingroup\$ Let me some time to build it. Anyway, I'm interested in how you would apply the ColliderDistance2D data to re-position the player when using Boxcast + Physics2.distance. Do you have any example of that? \$\endgroup\$ – Notbad Mar 21 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.