How to limit movement range?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dikawfa

I have KinematicBody2D and I want this node can move by player input.
The movement is very simple, it just need to slide/translate in the x axis.
How to give limit to an area, say 100 units in x axis in which the KinematicBody can move inside that 100 units area freely?

:bust_in_silhouette: Reply From: Asthmar

Using the clamp() function should work.

example :
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)

That code clamps the player to the screen size

It works, thank you…

dikawfa | 2020-10-20 14:08

1 Like