Help With Screen Clamping

Please paste your code here instead of using a screenshot. You can use the </> button here to wrap the code in triple backticks (```) to format it properly.

Firstly, I feel like you might prefer to set up some physical walls with collision shapes, instead of relying on clamp(). You can use StaticBody2D nodes (with a WorldBoundaryShape2D) for this. Refer to the documentation here: Physics introduction — Godot Engine (stable) documentation in English

But if you want to use clamp, I suspect your problem is related to how get_viewport().size works. The viewport size is the size of physical pixels on the screen that the actual window takes up. However, this might not match your game’s actual units, for example if you apply any zoom or stretch/maximize the window.

So, to get the actual size of the visible area, you should use get_viewport_rect().size instead.

Additionally, you should perform the clamping after calling move_and_slide(), since move_and_slide() will change the position. Otherwise your character will appear to sink into the edges a bit if the player holds a direction.