1
\$\begingroup\$

Consider a simple 2D platformer. I have a BoxCollider2D on the player object and a TilemapCollider on the ground below(grid). I created a "jump" script for the player through which the player object jumps and comes back to the ground, clean. But when the player object goes relatively high and comes down with a high velocity, it doesn't collide with the ground(grid) instead it goes right through the tilemap collider and into the void.

How can this behaviour be prevented?

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

To prevent a collider from passing through another collider at high velocity, change the "Collision Detection" option on its "Rigidbody 2D" from "Discrete" (default) to "Continuous".

Inspector of Rigidbody 2D

Note : Continuous collision detection has overhead and will have impact on the engine performance, but will provide a much better collision detection system.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ FYI, CCD casts a ray between the last and current position, checking against all objects whose bounding boxes intersect the bounding box of that ray. This is why it will never miss anything it should collide with. Having said that, it is more expensive than the purely iterative approach taken by discrete CD, since the latter checks only the current position, rather than along a ray. \$\endgroup\$
    – Engineer
    Dec 1, 2020 at 11:49
1
\$\begingroup\$

In the 2D world, changing your collider from discrete to continuous is not going to fix your problem if you're moving the wrong way. You also have to make sure that you are using the MovePosition command of your rigidbody (not using .position), or a super high velocity will still go through even a continuous collider.

\$\endgroup\$
2
  • \$\begingroup\$ I hope your name refers to the most excellent Fixx song, Saved by Zero. :D \$\endgroup\$
    – Almo
    Aug 24, 2023 at 20:20
  • 1
    \$\begingroup\$ Indeed it does @Almo! \$\endgroup\$ Aug 24, 2023 at 23:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .