What is the process for animating the rotation of my FBX model using the animate function?

I'm trying to figure out how to create a continuous rotation for my FBX model using three.js.

Here's what I've attempted so far:

  1. Declared my object as a variable
  2. Called my object in the animate function for rotation (girl1)

However, I encountered an error message:

Error: Cannot read property 'rotation' of undefined.

This is the snippet of my code:

function init() {
    ...

    // zombie girl

    let loader_girl = new FBXLoader();

    loader_girl.load("ZombiePunching.fbx", (object) => {
        // animation mixer
        mixer = new THREE.AnimationMixer(object);

        const action = mixer.clipAction(object.animations[0]);
        action.play();

        // make materials opaque
        object.traverse((child) => {
            if (child.isMesh) {
                child.material.transparent = false;
            }
        });

        object.scale.set(0.05, 0.05, 0.05);
        object.rotation.x = Math.PI;
        scene.add(object);

        girl1 = object;
    });

    ...
    let loader = new THREE.TextureLoader();
    loader.load("smoke.png", function (texture) {
        let cloudGeo = new THREE.PlaneBufferGeometry(500, 500);
        let cloudMaterial = new THREE.MeshLambertMaterial({
            map: texture,
            transparent: true,
        });
        for (let p = 0; p < 25; p++) {
            let cloud = new THREE.Mesh(cloudGeo, cloudMaterial);
            cloud.position.set(
                Math.random() * 800 - 400,
                500,
                Math.random() * 500 - 450
            );
            cloud.rotation.x = 1.16;
            cloud.rotation.y = -0.12;
            cloud.rotation.z = Math.random() * 360;
            cloud.material.opacity = 0.6;
            cloudParticles.push(cloud);
            scene.add(cloud);
        }
    });
}

function animate() {
    cloudParticles.forEach((p) => {
        p.rotation.z -= 0.002;
    });

    rainGeo.vertices.forEach((p) => {
        p.velocity -= 0.1 + Math.random() * 0.1;
        p.y += p.velocity;
        if (p.y < -200) {
            p.y = 200;
            p.velocity = 0;
        }
    });
    rainGeo.verticesNeedUpdate = true;
    rain.rotation.y += 0.002;

    girl1.rotation.y += 0.001;

    if (Math.random() > 0.93 || flash.power > 100) {
        if (flash.power < 100)
            flash.position.set(Math.random() * 400, 300 + Math.random() * 200, 100);
        flash.power = 50 + Math.random() * 500;
    }

    requestAnimationFrame(animate);

    renderer.render(scene, camera);
}

init();
animate();

Answer №1

Consider this alternative approach:

girl1.rotation.y += 0.001;

Here's a more robust solution:

if ( girl1 ) girl1.rotation.y += 0.001;

It is important to note that when beginning the animation loop prior to the FBX being fully loaded, the variable girl1 may be undefined for a period of time.

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

issue with non-existent value in v-for loop when using functional template refs

Scenario I am currently developing a personalized filtering feature. This feature allows users to add n filters that are shown using a v-for loop in the template. Users have the ability to modify input values and remove filters as needed. Challenge Issue ...

What is the best way to format a date input field so that when a user enters a year (yyyy), a hyphen (-

Need help with date formatting in the format yyyy-mm-dd. Seeking a way to prompt user input for the year and automatically append "-" to the date as needed. Also utilizing a datepicker tool for selecting dates. ...

Checking Sudoku Solutions on Codewars

I have come across this JavaScript code which seems to be functioning correctly. However, I am curious about the line board[3][8] != board[8][3] and how it checks for repeating row and column numbers. Can someone please provide an explanation? Thank you! ...

Encountering an issue with using third-party APIs due to receiving an error message stating that the requested resource does not have the 'Access-Control-Allow-Origin' header

My current challenge involves attempting to access a third-party API using AngularJS. Interestingly, I only encounter this issue in Chrome, as it works perfectly fine in IE. The error message that pops up reads as follows: XMLHttpRequest cannot load https ...

Determine the number of stored values in a specific key by utilizing stringify within localstorage

Is there a way to determine the number of unique values stored under one key in local storage? For instance: Here is the content of my local storage: [{"id":"item-1","icon":"google.com"},{"id":"item-2","icon":"youtube.com"}] In this case, I would like ...

I have chosen not to use the hashHistory plugin for one specific reason: the URLs are suffixed with hashes

index.js(previous) import React from 'react' import { render } from 'react-dom' import { Router, Route } from 'react-router' import App from './modules/App' render(( <Router history={hashHistory}> <R ...

Unlocking the Power of ReactJS: Passing Values in Material UI for Advanced JSON Structures

How can I access complex objects in the GRID component of material UI? Specifically, I am trying to access the ami_info.account, but it only displays Undefined in the UI. var columns = [ { field: 'id', headerName: 'ID', width: 90, ...

Displaying elements of array in Pug template is a key task for

As a newcomer to the Jade/Pug template engine used in Express, I am faced with a challenge. I need to display the name property of each object within an associative array that is passed as a parameter to my pug template from an express route module. I hav ...

Investigate duplicate elements within nested arrays using the 3D array concept

I am dealing with an array that contains 3 subarrays. Arr = [[arr1],[arr2],[arr3]] My task is to identify and store the duplicate values found in these subarrays ([arr1],[arr2],[arr3]) into a separate array. Arr2 = [ Duplicate values of arr 1,2,,3 ] What ...

Tips for declaring the OpenLayers map object globally

I have successfully created a map with ol 6 and added an OSM layer. Now I am trying to incorporate a vector layer that is coded in another JavaScript file, using the same 'map' object. Despite my efforts to declare it globally, it doesn't se ...

Tips for transferring contact information from a webpage to an Android or iPhone using an HTML button and integrating JavaScript or PHP

My current project involves adding a feature to a website that allows users to save contact details from the site to their phone (Android or iPhone) with the click of an HTML button. Here's what I have done so far <!DOCTYPE html> <html lang= ...

What is causing the array elements to be iterated through multiple times?

My goal is to display all the titles from an array called 'title element' containing 10 values. However, I am encountering a problem: The for loop outputs all 10 values repeatedly 10 times. The titles are: Title 1, Title 2, Title 3, Title 4, T ...

"Stored on the server: A preview of a musical masterpiece

I am working on a website concept where users can preview a 30-second snippet of a song before making a purchase decision. My plan is to store all the mp3 files below the root folder to prevent direct access. My main concern is whether I can enable the 30 ...

Is there a way to enable drag and drop functionality on a dynamically created table using AJAX?

I have been successfully using the TableDnD plugin for drag and drop functionality on table rows. However, I am now facing an issue with dynamically generated tables via AJAX. The code doesn't seem to work as expected when it comes to drag and droppin ...

Actions for jQuery's mouseenter and mouseleave events

I've implemented a jQuery script that controls the visibility of elements based on mouse events: $("#divid").mouseenter(function() { $('#divid').show(1000); }).mouseleave(function() { $('#divid').hide(1000); }); $("#hldiv" ...

Combining AngularJS with ng file upload and Sails JS for seamless file uploading

When I upload a file, I also need to send some additional information along with it. The instructions mention using the data parameter for this purpose, but I'm having trouble accessing it in my Sails controller action. Frontend: Upload.upload({ url ...

The EffectComposer in Three.js seems to be malfunctioning when combined with the Bloom

I'm completely stumped trying to troubleshoot the issue with these two mysterious .js files in my project. Despite the lack of documentation, I have implemented the EffectComposer and BloomPass and attempted to call them like this: parameters = { blo ...

JavaScript library imports function correctly in Chrome but encounter issues in Internet Explorer

I'm currently working with ASP.NET MVC 5 and encountering an issue when trying to incorporate various JavaScript libraries such as jQuery, bootstrap.js, and others. While it's functioning well in Chrome, I'm facing compatibility problems in ...

"Endowed with improper dimensions, the BootStrap collapse feature

Yesterday, I posted about an issue with BootStrap and panel collapsables causing graph sizes to become distorted. The post was locked because there was no accompanying code. I have now created a code snippet for you all to see the exact problem I am facing ...

Encountered an error while building CSS Modules for a Vue.js project

Working on a project in Vue.js, I decided to incorporate CSS modules for my local styles (for sass and scss). However, I continuously encounter a 'Failed to compile error' related to the CSS Loader validation process. Despite attempting various ...