#Custom KB

1 messages · Page 1 of 1 (latest)

vestal jetty
#

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.

leaden veldt
#

That s good

wispy citrus
#

where those number coming from

vestal jetty
#

I got the numbers by testing with another player to find a good configuration.

bright lantern
hushed sonnet
#

But yes it can be you’ll just need to configure the numbers enough to do so

nocturne spindle
#

@vestal jetty Bro U are so kind!!! I'm also trying to make and looking for it but I can never achieve the effect I want ,you are helping me so big!!!

vestal jetty
velvet pine
nocturne spindle
deep narwhal
#

Heyyy! can somebody help me, I have a question about knockbakc, I am making an addon and want if you get the tag knockback, that you get pushed into the air for 10 block, then the tag removes, can somlebody help me? Thanks!

deep narwhal
#

it's fixed!

lunar silo
#

anyone know how to stop them from going so far back i was testing it on a zombie

vestal jetty
#

edit horizontalKB

#

Edit horizontalKB for pushback

#

And vertical KB is for upward thrust

lunar silo
lunar silo
vestal jetty
#

There may be a mistake

#
    const horizontalForce = {
      x: knockbackX * horizontalKB,
      z: knockbackZ * horizontalKB
    };

    victim.applyKnockback(horizontalForce, verticalKB);
vestal jetty
lunar silo
#

its giving me this error

vestal jetty
#

Does it throw an error at you?

#

If you are testing with a non-player entity, the KB will not apply.

#

remove this "if (!(attacker instanceof Player)) return;"

#

@lunar silo

lunar silo
#

Yea the last line errors

lunar silo
lunar silo
#

@vestal jetty

earnest helm
lunar silo
earnest helm