I have a simple game that has one projectile Mesh colliding with an enemy Mesh. I am using a CreateAndStartAnimation:
Animation.CreateAndStartAnimation( "anim", projectile, "position", 30, 4, player.position,
enemyCoordinates, Animation.ANIMATIONLOOPMODE_CONSTANT, undefined,
() => { if (pickedEnemy)
if ( collideEnemyAndProjectile( pickedEnemy, projectile ) )
{ enemyList.forEach((enemy) => {
if ( pickedEnemy &&pickedEnemy.id == enemy.getEnemyId() ) enemy.damage(34);
if (!enemy.isAlive()) { pickedEnemy?.dispose();
enemy.destroy(); }
});}
projectile.dispose(false, true);
}
);
With my current implementation, the projectiles don’t always trigger a collision when they do hit (maybe 80% of the time it works). My guess is because this is an animation of the sphere and it is not present on the screen for every single frame?
Also, the other problem, is that the projectile does not always shoot to the point where the enemy is in real time; it shoots to where the enemy was. This is what the collideEnemyAndProjectile function looks like:
export const collideEnemyAndProjectile = (
enemy: AbstractMesh,
projectile: Mesh
) => {
if (projectile.intersectsMesh(enemy, true)) {
return true;
} else {
return false;
}
};
Is the “intersectsMesh” function the best one to use here?
Any feedback is very much appreciated. Thanks in advance!
Oh and if any of the code doesn’t make sense, feel free to ask. I am happy to respond!