Currently, I am working on a real-time game using three.js and websockets. The project has been running smoothly until recently when I encountered a hurdle. While implementing animations, I noticed that the animations for the opposing client on the web page are stuck on a single frame. This seems odd considering both clients are supposed to use the same set of animations. Below is a snippet of my code focusing on loading and updating the animations:
Loading Function:
function AddPlayer(pid){
//pid represents player id
players.push({
three: new THREE.Object3D()
});
//All elements load correctly
loader.load("Models/ybot.glb", function(gltf){
var examp = gltf.scene;
anim[ids.indexOf(pid)] = {idle: null, walk: null, leftPunch: null, rightPunch: null};
players[ids.indexOf(pid)].three = examp;
scene.add(players[ids.indexOf(pid)].three);
mixers[ids.indexOf(pid)] = new THREE.AnimationMixer(players[ids.indexOf(pid)].three.children[0]);
loader.load("Models/Animations/Idle.glb", function(glanim){
var idle = glanim.animations[0];
anim[ids.indexOf(pid)].idle = mixers[ids.indexOf(pid)].clipAction(idle);
anim[ids.indexOf(pid)].idle.play();
});
loader.load("Models/Animations/Walking.glb", function(glanim){
var walk = glanim.animations[0];
anim[ids.indexOf(pid)].walk = mixers[ids.indexOf(pid)].clipAction(walk);
anim[ids.indexOf(pid)].walk.play();
});
loader.load("Models/Animations/RightPunch.glb", function(glanim){
var punch = glanim.animations[0];
anim[ids.indexOf(pid)].rightPunch = mixers[ids.indexOf(pid)].clipAction(punch);
anim[ids.indexOf(pid)].rightPunch.play();
});
loader.load("Models/Animations/LeftPunch.glb", function(glanim){
var punch = glanim.animations[0];
anim[ids.indexOf(pid)].leftPunch = mixers[ids.indexOf(pid)].clipAction(punch);
anim[ids.indexOf(pid)].leftPunch.play();
});
});
}
Update function:
function UpdateAnim(){
for (var i = 0; i < players.length; i++) {
if (currAnim[i] == "idle"){
anim[i].idle.weight += 0.1;
anim[i].walk.weight -= 0.1;
if (anim[i].idle.weight >= 1){
anim[i].idle.weight = 1;
anim[i].walk.weight = 0;
}
}else if (currAnim[i] == "walk"){
anim[i].idle.weight -= 0.1;
anim[i].walk.weight += 0.1;
if (anim[i].walk.weight >= 1){
anim[i].idle.weight = 0;
anim[i].walk.weight = 1;
}
}
mixers[i].update(clock.getDelta());
}
}
Please forgive me if some variable names make it difficult to follow.