# \[SOLVED\] Limit camera movements

**URL:** <https://forum.playcanvas.com/t/solved-limit-camera-movements/6860>\
**Category:** Help & Support\
**Created:** [May 7, 2018, 11:03am UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860 "2018-05-07T11:03:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwayn](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Dwayn](https://forum.playcanvas.com/u/Dwayn)\
**Post date:** [May 7, 2018, 11:03am UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860/1 "2018-05-07T11:03:34Z")

</div>

Hi! I’m trying limit the camera movement in Y axes, my code:

> 

MouseInput.prototype.pan = function(screenPoint) {  
var fromWorldPoint = MouseInput.fromWorldPoint;  
var toWorldPoint = MouseInput.toWorldPoint;  
var worldDiff = MouseInput.worldDiff;  
var lastPos = new pc.Vec3();  
// For panning to work at any zoom level, we use screen point to world projection  
// to work out how far we need to pan the pivotEntity in world space  
var camera = this.entity.camera;  
var distance = this.orbitCamera.distance;

```
var yPos = this.entity.getPosition().y;
var lastYpos = pc.math.clamp(y,0.6,3);
if(lastYpos > 0.6){
    this.lastPos = this.entity.getPosition();
    camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
    camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);

    worldDiff.sub2(toWorldPoint, fromWorldPoint);
    this.orbitCamera.pivotPoint.add(worldDiff); 
}else{
    console.log("1 "+this.lastPos +" "+ this.entity.getPosition());
    this.entity.translate(this.lastPos.x, this.lastPos.y + 0.5, this.lastPos.z);
    console.log("2 "+this.lastPos +" "+ this.entity.getPosition()); 
}

```

The problem is, when i’m doing pan the Y is lower than the condition and never enter, i’m tried to set a position and don’t work, any idea?

Sorry for my english 🙂

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [May 7, 2018, 12:23pm UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860/2 "2018-05-07T12:23:15Z")

</div>

> [@Dwayn](#):
>
> ```javascript
> var lastYpos = pc.math.clamp(y,0.6,3);
> if(lastYpos > 0.6){
> 
> ```

If you are doing the clamp before this if statement, it will never enter the statement because it is always going to be 0.6 or lower.

Is the Y clamp relative to anything or do you know exactly where in world space you want to clamp it between?

---

<div class="post-metadata">

**Author:** ![Dwayn](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Dwayn](https://forum.playcanvas.com/u/Dwayn)\
**Post date:** [May 7, 2018, 1:03pm UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860/3 "2018-05-07T13:03:27Z")

</div>

Sorry, this is the code

> var lastYpos = pc.math.clamp(yPos,0.6,3);  
> if(lastYpos \> 0.6){

My camara starts the scene in 0,1,0 (world space) and my idea is limit the Y until 0.6

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [May 7, 2018, 1:15pm UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860/4 "2018-05-07T13:15:33Z")

</div>

Ahh… I misread the code.

I assume you are using Model Viewer template?

If you are trying to stop the camera from going above a certain height then it’s a bit tricky as the orbit camera is based on rotation around the model (pitch, yaw) and distance from the pivot point.

You can change the pitchAngleMin/Max values to achieve a similar effect?

---

<div class="post-metadata">

**Author:** ![Dwayn](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@Dwayn](https://forum.playcanvas.com/u/Dwayn)\
**Post date:** [May 7, 2018, 1:52pm UTC](https://forum.playcanvas.com/t/solved-limit-camera-movements/6860/5 "2018-05-07T13:52:51Z")

</div>

Nice! It’s work thanks!
