As the object rotates, the camera in Three.js dynamically follows suit, creating a

In my game, I am struggling to make the camera always face the back of a helicopter as it rotates. Despite my efforts, I can't seem to synchronize the camera's movement with that of the helicopter. You can see an image of what I'm aiming for here, and what is actually happening here.

The camera is initialized using the following code: 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 is called within my render loop, where 'movingObject' represents the helicopter. The rotation logic for the helicopter is found in my inputController function which is also called in the render loop. Here is the snippet of the rotation code:

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

I attempted to adjust the camera rotation by adding "camera.rotation.y += -0.05", but this did not yield the desired outcome.

Answer №1

To create the illusion of a camera following a helicopter, you can set up your helicopter to be a child of the camera. This way, your input controller can control the movement of the camera, giving the appearance that the camera is tracking the helicopter's movements (even though it's actually the other way around). In your initialization function, you can do the following:

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

For more insights on positioning objects in front of the camera using THREE.JS, check out this helpful resource: How to put an object in front of camera in THREE.JS?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to apply styling to a Vue.js template via a function

Trying to create a custom style text using a function in vue.js <template> <div class="col-md-3" :style="setColor(c.percentage, c.blocked)"> </template> <script> export default { name: "& ...

Struggling with mapping through a multidimensional array?

I'm facing an issue with using .map() on a nested array. I initially tried to iterate through my stored data using .map(), and then attempted another iteration within the first one to handle the nested array, but it didn't work as expected. The ...

Is there a way to send a personalized reply from an XMLHttpRequest?

My goal is to extract symbol names from a stocks API and compare them with the symbol entered in an HTML input field. I am aiming to receive a simple boolean value indicating whether the symbol was found or not, which I have implemented within the request. ...

Images failing to load in jQuery Colorbox plugin

I am having an issue with the Color Box jQuery plugin. You can find more information about the plugin here: Here is the HTML code I am using: <center> <div class='images'> <a class="group1" href="http://placehold.it/ ...

Utilizing React for incorporating HTML5 canvas gradients

I'm currently working on an app that allows users to generate a background gradient with two distinct colors using React. The issue I'm facing is that while the first color appears correctly, the second color ends up looking more like a solid col ...

Event handlers in JQuery are not connected through breadcrumb links

Wondering how to ensure that the click handler is always attached in my Rails 4.1 app where I am using JQuery-ujs to update cells in a table within the comments#index view. In my comments.js.coffee file, I have the following code snippet: jQuery -> ...

Separating Main Page Content from Sidebar for Independent Scrolling

In the layout of our website, there is a narrow column on the left containing menu options, while the right side is occupied by the main page content. The challenge I am facing is figuring out how to ensure that the page content can scroll independently ...

Implementing an active class in Vue.js for the router-link component

I am facing an issue with my sidebar item becoming inactive when I click on a sublink inside a component. How can I prevent the active class from switching off? Here is my sidebar: <router-link to='/sub/success_tools_subscriptions' ...

What is the method of direction storage used by Three.js?

element, there is a discussion about coding a server for a new game developed by a group of friends. The question focuses on how to store the direction a player is looking at in a 3D plane efficiently. One suggestion is using an object with two variables ...

Error: The current component does not have a template or render function specified. Check the <App>

I am a beginner in Vue js and I am facing an issue while running my application. It is showing an empty page with the error message: Component is missing template or render function. at <App>. Additionally, there is also a warning from Vue Router sa ...

Ways to properly update a state variable when using onClick within a functional component

I'm having an issue with my tabs that are supposed to display different products and filters when clicked. Currently, I have to click each tab twice before the 'options' list updates with the correct filters. I know that calling setOptions w ...

The AdminLTE Bootstrap Table Search Bar vanishes when extra <th> tags are added

Currently facing difficulties with the table functionality in AdminLTE. By default, AdminLTE includes a search and organization feature in its table structure. When I insert some table data using PHP, everything looks fine. However, when I attempt to add ...

Eliminate the excess padding from the Material UI textbox

I've been struggling to eliminate the padding from a textbox, but I'm facing an issue with Material UI. Even after setting padding to 0 for all classes, the padding persists. Could someone provide guidance on how to successfully remove this pad ...

Vue app showcasing array items through search upon button pressing

As I delve into learning Vue Js, I've encountered a bit of confusion. My goal is to showcase array elements from a Rest API based on the specific ID being searched for. For example: within my Json File are various individuals with unique IDs, and when ...

Designing a slider to display a collection of images

I am currently working on a slider project using HTML, javascript, and CSS. I'm facing an issue where only the first two images (li) are being displayed and it's not transitioning to the other (li) elements. Below is the HTML code snippet: <se ...

Unable to locate the 'react-native' command, attempted various fixes but none were successful

Working on an older react native project that was functioning perfectly until I tried to pick it back up and encountered a problem. https://i.stack.imgur.com/1JUdh.png This issue revolves around the package.json file. https://i.stack.imgur.com/v6ZEf.png ...

animation of leaping to a specific element

I am currently working on a sidebar with links that have a hover effect to add a bullet. However, I want the bullet to smoothly follow the cursor's movement along the y-axis within the sidebar instead of jumping between the links. How can I achieve th ...

Is there a way to prevent the Pace js plugin from running on page load, but still have it execute during Ajax requests only?

I have successfully implemented the jquery pace plugin with a progress bar theme. Everything is working well, but I am looking to make it run only on ajax requests. I have tried various solutions found through research, but haven't had any luck. Belo ...

Creating TypeScript declarations for standard JavaScript functions and objects so they can be accessed in a TypeScript project

In my TS project, I am currently using Node, Express, and Handlebars along with some client-side JS successfully. I don't have any other client-side frameworks like React or Angular integrated at this time. Recently, I have been thinking about conver ...

Eliminating 'related' elements with jQuery

I am facing an issue with deleting items in a list. The list contains two types of products, product-x and product-y. Whenever I delete one type of product, I want the equivalent product of the other type to also be deleted. I have tried different approac ...