Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a helicopter in my game, I want the camera to always look at the back of the helicopter but I can't do it. When the helicopter rotates I cant make the camera rotate with the helicopter. Here is the image of the rotation of the helicopter and camera.This is what I want even the helicopter rotates. But this is what happened \

Camera initiliazion : const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

function cameraUpdate(camera) {
  camera.position.x = movingObject.x + 20
  camera.position.y = movingObject.y + 20;
  camera.position.z = movingObject.z;
  camera.lookAt(movingObject.position);}

This function called in my render loop. movingObject is my helicopter. The rotation code of the helicopter is in my inputController function which is called in the render loop. The rotation snippet is below.

if (input.rotClk === true) {
    moveableObj.rotatation.y += -0.05;
}

I try camera.rotation.y += -0.05 and It did not work.

question from:https://stackoverflow.com/questions/65646553/three-js-rotate-the-camera-when-the-object-rotate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
224 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can make your helicopter a child of the camera and then make your input controller control the camera, and it will look like the camera is following the helicopter (when in reality the helicopter is following the camera, just translated forward and down) So, in your init function:

camera.add(movingObject);
movingObject.position.set(0,-10,-100);
scene.add(camera)

This answer might have more information How to put an object in front of camera in THREE.JS?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...