How to create camera restriction

Hey! I intend to create a restriction for my camera(which follows player using lockedTarget), so that it will not go beyond certain boundaries(probably a square) that marks the perimeter of my map(consider it as a ground mesh). How shall I achieve this? Any help is appreciated!

1 Like

What type of camera? Possibly set conditions on camera position, e.g. if camera.position.x > 10 then camera.position.x = 10. Possible set conditions on the player?

A basic playground of your setup would help us answer the question.

1 Like

Hi JohnK!

Thank you for replying!
I’m new to Babylon, so I’m currently using a basic FreeCamera. But any other kinds are welcome if they can do better on this case. The x,z position constraints may not work since my environment model is a square that is intended to rotate 45 degrees, but the camera is not rotated. I created a playground that may help you understand the issue. Thank you again!

1 Like

This is one method, it may not be entirely what you want. The avatar is restricted to the ground and so the camera is restricted.

Please explain further if you need something different.


This is amazing! Thank you!
I’m trying to achieve a bit further, which is to stop the camera from moving when the ground starts to disappear. And retarget player when the player goes back. I adjusted based on your design. The re-targeting dosn’t work yet, but it may better explain my goal(When you reach the edge, the camera stop first). I tried to re-target lock on player afterward, but the camera angle changes too.

In a nutshell, my main goal is to hide everything not ground from the user and keep the camera angle at the same time.

Thank you !

Hello @Harvey_Li just checking in, was your question answered?

Not quite, I’m still looking for a solution based on my demo above to solve the re-targeting

Just to check on my understanding, when you say “retargeting”, you mean that you want the camera to be centered on the player at all times, like this:

But after the player gets to a boundary and comes back, the camera is not centeres on it anymore:

And that’s what you’d like to fix?

Yes! I would like to get the camera back to where the player is centered.

Have you tried using a follow camera? Camera Introduction | Babylon.js Documentation (

1 Like

Yes, I had. But an effect I don’t want to have is that the follow camera always has a gradual camera angle transformation when the user moves.

You could parent the camera to the avatar so that it moves alongside it, and set the camera’s target as the avatar: JoystickWalkerW/Camera | Babylon.js Playground (