I just found orbitCamera in Model Viewer Starter Kit is quite useful, but what I wanna do with it is to set a limitaion on it.
For example, the camera is not allowed to get to the underground, so the camera’s position.y needs to be > 0
I put the code below in the function OrbitCamera.prototype._updatePosition
if(position.y <= 0){
position.y = 0.1;
}
this.entity.setPosition(position);
When I test it, the camera indeed doesn’t go to underground positions no matter how I move the mouse.
The scene of the problem is,
I keep the right mouse button down and moving the mouse up for seconds so the camera can get down to the ground definitely. Because the code above, the camera slides on the ground and won’t go downward anymore after it gets to the ground,
Then I still keep the right mouse button down and moving the muse down, the camera won’t go up immediately. It feels like sticking to the ground for a while, and then will go up…
I guess the problem is I need to change some delta vals to 0 when the camera gets to ground… I tried some vals but still doesn’t work…
Can anyone plz help?
The link to the project is https://playcanvas.com/project/482357/overview/ffff
SOLUTION
I just checked the orbitcamera and related input script, and finally make a solution. In mouseInput script,
MouseInput.prototype.pan = function(screenPoint) {
var fromWorldPoint = MouseInput.fromWorldPoint;
var toWorldPoint = MouseInput.toWorldPoint;
var worldDiff = MouseInput.worldDiff;
// 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;
camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
worldDiff.sub2(toWorldPoint, fromWorldPoint);
// If the camera entity is very close to the ground, make sure the diff.y is positive, so the camera will move up
if(this.entity.getPosition().y < 0.1){
worldDiff.y = 0.1;
}
this.orbitCamera.pivotPoint.add(worldDiff);
this.oldWorldDiff = worldDiff;
};
MouseInput.prototype.onMouseMove = function (event) {
var mouse = pc.app.mouse;
if (this.lookButtonDown) {
// If the camera is very close to the ground, pitch it up
if(this.entity.getPosition().y < 0.1){
this.orbitCamera.pitch -= 0.1;
}
else{
this.orbitCamera.pitch -= event.dy * this.orbitSensitivity;
}
this.orbitCamera.yaw -= event.dx * this.orbitSensitivity;
} else if (this.panButtonDown) {
this.pan(event);
}
this.lastPoint.set(event.x, event.y);
};
The logic is quite simple, if the camera moves according to the delta vals like worldDiff, event.dy… So if the camera is very close to the ground, force these delta vals to make the camera go up. Simple, but still not a perfect solution tho…