# How to prevent a camera\`s target getting out of cube?

**URL:** <https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535>\
**Category:** Questions\
**Tags:** arcrotatecamera\
**Created:** [December 13, 2022, 8:10pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535 "2022-12-13T20:10:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olesya\_Polyanskaya](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/olesya_polyanskaya/32/28731_2.png) [@Olesya\_Polyanskaya](https://forum.babylonjs.com/u/Olesya_Polyanskaya)\
**Post date:** [December 13, 2022, 8:10pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/1 "2022-12-13T20:10:54Z")

</div>

In my project I use ArcRotateCamera and also I have 50 meshes and a big cube, that is a parent for those 50 meshes. So I need to prevent my camera`s target from getting out of the cube. Is there any possibility to do this? I tried to change the target`s position when its out of the cube, but then I have a visual problem, my camera is jumping. I know why but I don`t need this “side effect”.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 14, 2022, 11:17am UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/2 "2022-12-14T11:17:01Z")

</div>

Welcome aboard!

Are you able to share your code in the [Playground](https://playground.babylonjs.com/)?

It will be a lot easier for people to help you.

Maybe one thing you can try is enabling [collisions](https://doc.babylonjs.com/features/featuresDeepDive/cameras/camera_collisions)?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 14, 2022, 5:02pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/3 "2022-12-14T17:02:57Z")

</div>

cc @PolygonalSun just in case you have experienced this already

---

<div class="post-metadata">

**Author:** ![PolygonalSun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/polygonalsun/32/2704_2.png) [@PolygonalSun](https://forum.babylonjs.com/u/PolygonalSun)\
**Post date:** [December 14, 2022, 5:56pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/4 "2022-12-14T17:56:34Z")

</div>

For this, I guess it would be important to know how you have things set up. For example, how are you assigning/updating your camera’s target? Also, what do you mean when you say your camera is “jumping”? Is it erratically moving, teleporting, or is it being pushed in a specific direction? The easiest way for us to provide insight into what’s going on would be to see it in action in a playground or at least to see a code snippet so if you could post something in the thread, that would help us to know what’s going on.

---

<div class="post-metadata">

**Author:** ![Olesya\_Polyanskaya](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/olesya_polyanskaya/32/28731_2.png) [@Olesya\_Polyanskaya](https://forum.babylonjs.com/u/Olesya_Polyanskaya)\
**Post date:** [December 18, 2022, 6:29pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/5 "2022-12-18T18:29:29Z")

</div>

Here is my code on PG [https://playground.babylonjs.com/#ZSQ5DZ#147](https://playground.babylonjs.com/#ZSQ5DZ#147)  
A grey sphere in the center of a cube is my camera`s target. An almost transparent cube is a parent for whole red spheres that inside it. About "jumping camera", camera`s target is always in the center of the camera, but when I move the target like on 200 px to the right, its not in the center anymore, that`s way the camera chages its position and that`s way I have this 'jumping" effect. But its not my main issue. My main issue is to prevent the traget to getting out of parent cube while i`m zooming or panning.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [January 17, 2023, 11:57am UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/6 "2023-01-17T11:57:47Z")

</div>

Hey! Sorry we didn’t see this before. @PolygonalSun do you know any nice ways to keep the camera’s target constrained inside an area?

EDIT: Oh, I just saw another answer of mine about a very similar situation that could be useful here: [Restrict camera position and apply elastic stop at edges - #2 by carolhmj](https://forum.babylonjs.com/t/restrict-camera-position-and-apply-elastic-stop-at-edges/36658/2). It concerns the camera’s position, but you could modify it to apply to the target instead.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [January 17, 2023, 12:57pm UTC](https://forum.babylonjs.com/t/how-to-prevent-a-camera-s-target-getting-out-of-cube/36535/7 "2023-01-17T12:57:49Z")

</div>

It might be odd to say and not what you are looking for but…Your targets are meshes, is it? And you want to constrain your meshes inside the cube, right? Then why not just enable and check the collision with your cube… Something like this, may be:

> **[Babylon.js Playground](https://playground.babylonjs.com/#ZSQ5DZ%23149)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Edit: Except you would need to invert the normals on the cube and remove your trick for repositioning the sphere outside of bounding box.
