How to restrict the camera like in a 2D game?

How can I restrict the camera like in a 2D game?
Something like it can move only from X 0 to X 100.

You clamp the coordinates.

cam               = scene.active_camera;
x, y, z           = cam.worldPosition;
cam.worldPosition = [max(0, min(x, 100)), y, z];

Could also put the clamping in a function to make it more readable.

def clamp(value, low, upper): return max(low, min(value, upper));

Though it’d be easier to maintain if you were to subclass KX_Camera and override a couple of methods to provide this functionality in an automatic fashion, rather than doing it “manually” everytime. That’s a tad more advanced so I dunno if you wanna get into it. I have a few files hanging around that more or less demonstrate the concept, but it’s not exactly easy to understand if you’re not used to working with classes in python.

1 Like

Tutorial that might help.

1 Like

It works perfectly, thank you.

These tutorials are really useful. Thank you.