What this does is that it modifies the KB of the players to try to assimilate the KB with servers. It is not the best but it works well
import { world, Player } from '@minecraft/server';
const config = {
horizontalKB: 0.2789,
verticalKB: 0.1023
};
world.afterEvents.entityHitEntity.subscribe((event) => {
const attacker = event.damagingEntity;
const victim = event.hitEntity;
if (!(attacker instanceof Player)) return;
if (!attacker || !victim) return;
const attackerLocation = attacker.location;
const victimLocation = victim.location;
let knockbackX = victimLocation.x - attackerLocation.x;
let knockbackZ = victimLocation.z - attackerLocation.z;
const magnitude = Math.sqrt(knockbackX * knockbackX + knockbackZ * knockbackZ);
if (magnitude !== 0) {
knockbackX /= magnitude;
knockbackZ /= magnitude;
}
let horizontalKB = config.horizontalKB;
let verticalKB = config.verticalKB;
const horizontalForce = {
x: knockbackX * horizontalKB,
z: knockbackZ * horizontalKB
};
victim.applyKnockback(horizontalForce, verticalKB);
});
Any suggestion will be gladly accepted as well as a good config if you find one.