- Home /
Adding boundaries to restrict rigidbody2D movement
I have a simple player 2D controller that moves a 2D kinematic rigidbody around the scene using MovePosition. I also have some Box Collider 2D setup around the scene, and these are meant to be boundaries of the map - player can't move beyond these colliders. These colliders also have a rigidbody 2D.
My problem arises when I try to get my player to collide with the colliders. If I set boundary rigidbody to Kinematic, the player moves through them; If I set it to Dynamic, the player pushes the boundaries around(I don't want to restrict XYZ to force it static); if I set it to Static the player moves through the boundary as well.
How do I get my player to not move through the boundary colliders? Think of them as walls.
Answer by Ady_M · Jan 04, 2019 at 12:43 AM
You may have to do it by Raycasting before moving. A kinematic object cannot be pushed by other objects.
If the boundaries are really simple (just 4 walls, for instance) and you don't need to move or rotate the boundaries then you could get away with simply clamping the position of the moving object.