import { world } from '@minecraft/server';
const normalize = (x, z) => {
const magnitude = Math.sqrt(x * x + z * z);
return magnitude === 0 ? { x: 0, z: 0 } : { x: x / magnitude, z: z / magnitude };
};
world.afterEvents.entityHitEntity.subscribe((event) => {
const attacker = event.damagingEntity;
const target = event.hitEntity;
if (!attacker || !target) return;
if (attacker.typeId === 'minecraft:player') {
const player = attacker;
const { x, z } = normalize(player.getViewDirection().x, player.getViewDirection().z);
if (player.isSprinting) {
const sprintSpeed = Math.sqrt(player.velocity.x ** 2 + player.velocity.z ** 2);
const horizontalKnockback = -0.2 * sprintSpeed;
const verticalKnockback = -0.1 * sprintSpeed;
target.applyKnockback(x * horizontalKnockback, z * horizontalKnockback, verticalKnockback, 0);
} else {
target.applyKnockback(0, 0, 0, 0); // Pas de knockback
}
}
});
console.log('Sprint Knockback v3');```
#knockback
1 messages · Page 1 of 1 (latest)
hi i have this error with this script
:
[Scripting][error]-TypeError: cannot read property 'x' of undefined at <anonymous> (sprintkb.js:25)
What version of the API are you using?
i think the error is here
const sprintSpeed = Math.sqrt(player.velocity.x ** 2 + player.velocity.z ** 2);```
Because this looks like code for 1.19 or lower
im using the new one
I think I'm using the most recent one
with this script im trying to get hive knockback and i want to delete or change the knockback when hitting
In that case, a few things:
- player.velocity should be getVelocity()
- applyKnocback takes an object: player.applyKnockback({x: 0, z: 0}, 5)
- Horizontal force and vertical force baked in
This is assuming you are using the newest one 
like the player.velocity should be player.getVelocity ?