Camera Resetting

There is an asteroid model that moves in an orbit, but when i click a button the camera zooms in on its position to be the main object in the scene. However, when I try zooming or rotating the camera reverts back to its view preventing any rotation or zooming. Any idea how to fix this in react three fiber?

Solution depends on the code you’re using for zooming and controlling the camera - unpossible to help without seeing related parts of the code.

2 Likes

function Asteroid(props) {
const asteroidRef = useRef();
const initialTime = useRef(props.initialTime);
const { camera } = useThree();

useFrame((state) => {
if (asteroidRef.current) {
const elapsedTime = state.clock.getElapsedTime();
const t = elapsedTime * speedFactor + initialTime.current;

  if(!props.zoomedIn){
    const newPos = calculatePosition(t);

  asteroidRef.current.position.set(...newPos);

  asteroidRef.current.rotation.x += 0.01;
  asteroidRef.current.rotation.y += 0.01;     
}

  else  {
   
    const newPos = asteroidRef.current.position;
    
    const targetPosition = new THREE.Vector3(...newPos).add(new THREE.Vector3(0, 0, 3)); 
    camera.position.lerp(targetPosition, 0.05);n
    camera.lookAt(asteroidRef.current.position);  

That was for zoomin in on the object