[SOLVED] Limit camera movements

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 :slight_smile:

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?

1 Like

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

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?

Nice! It’s work thanks!