I have two ideas.
Apply force on fireball attack
If a fireball hits a unit with enough health, the unit stands still. If it dies from the hit, it should be blown away by the impact to show the fireball’s power. Let’s calculate the angle between the object and the player’s direction:
const angle = Vector3.GetAngleBetweenVectors(shootedObject, playerDirection, new Vector3(0, 1, 0))
const impulseDirection = shootedObject.add(new Vector3(0, Math.sin(angle) * 0.1, 0)).normalize()
And then apply the impulse to the object:
physicsImpostor.applyImpulse(impulseDirection.scale(force), object.getAbsolutePosition())
object.actionManager.processTrigger(ActionManager.NothingTrigger, context)
Disappear after death
One more idea to slowly disappear units after death. You can run them async function.
For example MeshAnimator.ts
static class for call it from anywhere:
async animateVisibility(mesh: AbstractMesh, from: number = 1, to: number = 0, animationSpeedRatio: number = 1): Promise<void> {
return new Promise<void>((resolve) => {
if (mesh) {
const alphaAnimation = new Animation(
'alphaAnimation',
'visibility',
this.fps,
Animation.ANIMATIONTYPE_FLOAT,
Animation.ANIMATIONLOOPMODE_CONSTANT,
)
const keys = [
{ frame: 0, value: from },
{ frame: this.fps, value: to },
]
alphaAnimation.setKeys(keys)
mesh.animations.push(alphaAnimation)
const onAnimationEnd = () => {
if (alphaAnimation) {
mesh.animations = mesh.animations.filter((anim) => anim !== alphaAnimation)
}
animation.onAnimationEndObservable.removeCallback(onAnimationEnd)
resolve()
}
const animation = mesh.getScene().beginAnimation(mesh, 0, this.fps, false, animationSpeedRatio, onAnimationEnd)
} else {
resolve()
}
})
}
async animateSubMeshesVisibility(mesh: AbstractMesh, from: number = 1, to: number = 0, animationSpeedRatio: number = 1) {
return new Promise<void>(async (resolve) => {
let childMeshes = mesh.getChildMeshes()
for (let index in childMeshes) {
let subMesh = childMeshes[index]
await this.animateVisibility(subMesh, from, to, animationSpeedRatio)
}
resolve()
})
}
and call it from handler:
slowDispose(mesh: AbstractMesh) {
MeshAnimator.animateSubMeshesVisibility(mesh, 1, 0, 0.05).then(() => {
mesh.dispose()
})
}
fastDispose(mesh: AbstractMesh) {
MeshAnimator.animateSubMeshesVisibility(mesh, 1, 0, 0.5).then(() => {
mesh.dispose()
})
}
After unit death you can call slowDispose, and they will disappear smoothly. Looking for new updates. Can’t wait when we play together in this multiplayer game.