I want to be able to zoom the camera using buttons and I am making a test here Start , I am adding to the camera.position.z -= 0.05 in the render function, it works as expected but when I rotate the object using the OrbitControls the zooming is wrong, I don’t understand what is going on?
I am wondering why not this functionality is added by default in the OrbitalControls, I mean to be able to zoom in and out not just with the mouse wheel?
I tried and it works fine except when rotating the camera, when this happens is not good anumore.
pan(direction){
this.panObject = {panAmount: -.1}
// Get the current camera position and target
const currentTarget = this.controls.target.clone();
const currentPosition = this.activeCamera.position.clone();
// Calculate the new target position by panning left
const newTarget = currentTarget.clone();
newTarget.x -= this.panObject.panAmount; // Adjust this value to control the pan amount
// Calculate the new camera position based on the new target
const newPosition = currentPosition.clone();
newPosition.x -= this.panObject.panAmount; // Keep the same distance from the target
// Set the new target and camera positions
this.controls.target.copy(newTarget);
this.activeCamera.position.copy(newPosition);
the problem is fundamentally that you have two pieces of code pulling on the camera, which is a race condition. orbit controls is a sealed control with very limited functionality, if it causes the root issue i would suggest you don’t hack around it. let cc solve it. it operates like orbit controls (but smoother due to unity damping instead of ugly lerp), and it can also be controled programatically.
here’s a react example of this but it’s the same in vanilla, all you need to call is
I have tried camera controls but I am using a model and scaling it to fit the viewport and it messed it up, the scale is all wrong and I could not find a way to fix it…
You’re only ever setting the x axis of newPosition, you’ll need to do as @prisoner849 has suggested but add / subtract from the x and z position based on a camera “right” or “left” world direction eg something like…
const moveLeft = () => {
let dir = new THREE.Vector3();
camera.getWorldDirection(dir);
const left = new THREE.Vector3();
left.crossVectors(camera.up, dir).normalize();
newTarget.add(left)
newPosition.add(left)
}
const moveRight = () => {
let dir = new THREE.Vector3();
camera.getWorldDirection(dir);
const right = new THREE.Vector3();
right.crossVectors(dir, camera.up).normalize();
newTarget.add(right)
newPosition.add(right)
}
this is untested psuedo code and not complete ( i may have missed some steps in calculating the left and right vectors ( see this post by @prisoner849 ) and you may also want to use left.multiplyScalar(amount) to increase the amount to be moved ) but should put you in the right direction as to what you need to achieve…
here’s an adapted pen of @prisoner849’s including the above left and right calculations, it doesn’t include the tween but that shouldn’t be too difficult to accomplish at this stage…