How to restrict player movement not to walk out of the scene

Godot Version

Godot 4.2.2

Question

I have tried this method https://forum.godotengine.org/t/how-to-prevent-player-from-moving-outside-the-shape-or-area/23585 but the player still can move. How do I fix this? or Do I have to add the code in the player script?


Do you have the settings correct to ensure the player collides with the polygon?
And when you move the player, are you using move_and_slide() or are you manually changing their position?

Thank you so much. I forget to add move_and_slide() in the player script.

Also, if you have a Camera2D node on your player you can set the Top, Bottom, Left and Right boundaries in the Camera2D → Limit section of the inspector. This will keep the camera from moving out of the scene.

Those properties can also be set via code using the left_limit, right_limit, etc properties.

But it does sound like using move_and_slide will fix your initial problem of not having the collisions stop the character.

I hope this helps.

1 Like

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