Hello! I’m new to BabylonJS. I’m trying to smoothly change between animations. But you can see the effects and the main code below. Please let me know if I did something wrong.
const playAnimation = (currAnim: AnimationGroup, nextAnim: AnimationGroup = relaxMotion, nextLooping: boolean = true, preAnim?: AnimationGroup,) => {
console.log("currAnim.weight: ", currAnim.weight);
console.log("nextAnim.weight: ", nextAnim.weight);
if (preAnim) {
console.log("preAnim.weight: ", preAnim.weight);
}
// change anim
if (preAnim) {
preAnim.start(false)
}
currAnim.start(false)
const duration = currAnim.animatables[0].toFrame * 34 - 500
currAnim.loopAnimation = false
let preWeight = 1.0
let currWeight = 0.0
let obs = scene.onBeforeAnimationsObservable.add(function () {
preWeight -= 0.01;
if (preWeight <= 0) {
scene.onBeforeAnimationsObservable.remove(obs);
preWeight = 0.0;
currWeight = 1.0;
} else {
currWeight = 1.0 - preWeight;
}
if (preAnim) {
preAnim.weight = preWeight
} else {
relaxMotion.weight = preWeight
}
currAnim.weight = currWeight
})
// next anim
setTimeout(() => {
nextAnim.start(nextLooping)
let currWeight = 1.0
let nextWeight = 0.0
let obs = scene.onBeforeAnimationsObservable.add(function () {
currWeight -= 0.01;
if (currWeight <= 0) {
scene.onBeforeAnimationsObservable.remove(obs);
currWeight = 0.0;
nextWeight = 1.0;
} else {
nextWeight = 1.0 - currWeight;
}
currAnim.weight = currWeight
nextAnim.weight = nextWeight
})
}, duration);
}