#how do I apply knockback in the movement direction of an entity?

1 messages · Page 1 of 1 (latest)

dark monolith
#

Can anyone tell me how?

restive basalt
#

@dark monolith

const direction = entity.getVelocity();
entity.applyKnockback(direction.x,direction.z,1.25,0.3);

the 2nd last parameter is how far back it gets knocked back and the last one is how far up

dark monolith
restive basalt
#

length of a vector?

dark monolith
#

So basically speed is 1

#

Not sure how to say, but I want it same like set velocity

#

Ig setImpulse doesn't work on players

restive basalt
#

i cant remember who or where it was tho

restive basalt
#

maybe something else idk how the calcs work on knockback tbh

dark monolith
#

The question is where

restive basalt
dark monolith
#

🤔

#

Oh

restive basalt
#

but i might be wrong

#

idk

dark monolith
#

Hmm

whole bronze
dark monolith
#
const setVelocity = function(vector) {
    const horizontal = Math.sqrt(vector.x * vector.x + vector.z * vector.z) * 2.0;
    this.applyKnockback(vector.x, vector.z, horizontal, vector.y < 0.0 ? 0.5 * vector.y : vector.y);
};

Player.prototype.setVelocity = setVelocity;
Entity.prototype.setVelocity = setVelocity;```
#

ah

#

this ig

proven badgerBOT
#
Debug Result

There are 3 errors in this [code](#1134120578031304754 message):

<repl>.js:3:5 - error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

3     this.applyKnockback(vector.x, vector.z, horizontal, vector.y < 0.0 ? 0.5 * vector.y : vector.y);
      ~~~~

``````ansi
<repl>.js:6:18 - error TS2551: Property 'setVelocity' does not exist on type 'Player'. Did you mean 'getVelocity'?

6 Player.prototype.setVelocity = setVelocity;
                   ~~~~~~~~~~~

  @minecraft/server.d.ts:4956:13
    4956             getVelocity(): Vector3;
                     ~~~~~~~~~~~
    'getVelocity' is declared here.

``````ansi
<repl>.js:7:18 - error TS2551: Property 'setVelocity' does not exist on type 'Entity'. Did you mean 'getVelocity'?

7 Entity.prototype.setVelocity = setVelocity;
                   ~~~~~~~~~~~

  @minecraft/server.d.ts:4956:13
    4956             getVelocity(): Vector3;
                     ~~~~~~~~~~~
    'getVelocity' is declared here.

dark monolith
#

?????

#

Idk what even is wrong

#

How do I get the movement direction vector btw?

unreal granite
#

->player.applyKnockback(player.getVelocity().x * 2, player.getVelocity().z * 2, 35, 2);

#

that's what I did and it works

#

->if (player.getVelocity().x != 0 || player.getVelocity().z != 0) { player.applyKnockback(player.getVelocity().x * 2, player.getVelocity().z * 2, 35, 2); }

#

you could do that, if you want it to only happen if you have any movement

whole bronze
unreal granite
#

here's an improved version of the one I recently made. Which now does it, for other mobs, instead of the player. It also only works if the mob is on the ground, so it doesn't constantly keep knocking the mob back...

whole bronze
#

i dont think its about kb

unreal granite
#

I mean, the title says other wise lol

whole bronze
#

not "knock back"

#

its the applyImpulse for player that's needed, i think

unreal granite
#

what's the difference

#

between applyImpulse and knockback

whole bronze
#

all i know is that they have different inputs

unreal granite
#

but they pretty much do the same thing

dark monolith
#

wait wait wait

#

im confused now

#

whats correct?

#

i thought entity velocity normalized is entity movement vector

#

@whole bronze @unreal granite

unreal granite
#

now I'm confused

#

entity.getVelocity() is for how fast something is traveling I'm pretty sure

unreal granite
whole bronze
#

oh w8, ye, its not 1 magnitude

#

mb

#

use Vector and normalize it ig

dark monolith
dark monolith
#

when normalize its just direction

unreal granite
#

huh

unreal granite
#

or an entity

dark monolith
#

dw

unreal granite
#

The one I sent earlier does that already

#

See this

#

and you can change it however you like

#

lemme know if that works for you ig

#

if you use it, make sure it doesn't detect items, because for me it broke, and gave me errors

#

so replace minecraft:player with minecraft:item

dark monolith
#

Oh, but get velocity also returns speed

unreal granite
#

true

dark monolith
#

So when just want to have the vector without speed u just need to normalize it

#
import {
  system,
  world,
  Player,
  Entity,
  Vector
} from "@minecraft/server";


const setVelocity = function(vector) {
    const horizontal = Math.sqrt(vector.x * vector.x + vector.z * vector.z) * 2.0;
    this.applyKnockback(vector.x, vector.z, horizontal, vector.y < 0.0 ? 0.5 * vector.y : vector.y);
};

Player.prototype.setVelocity = setVelocity;
Entity.prototype.setVelocity = setVelocity;


let timer
let level
let oldRot

system.runInterval(() => {
  world.getAllPlayers().forEach((player) => {
    if (player.oldRot == undefined) {
        player.oldRot = player.getRotation();
    }
    if (player.level == undefined) {
               player.level = 0;
           }
    if (player.timer == undefined) {
               player.timer = 0;
           }
           
    player.timer++
    if (player.timer == 5) {
        player.level = 0;
        player.timer = 0;
    }
    
    if (player.isJumping){
       player.timer = 0;
       let newRot = player.getRotation();
       let difference = newRot.y - player.oldRot.y;
       player.oldRot = player.getRotation();
       if (difference >= 45 || difference <= -45) {
           
           if (player.level != 3) {
           player.level++
           }
           
           let movementVec = Vector.normalized(player.getVelocity());
           let newVelocity = ((player.level-1)*1);
           const vec = Vector.multiply(movementVec, newVelocity);
           player.setVelocity(vec);
           
       }
    }
    
  });
}, 2);
#

Damn, the bot doesn't work

#

@unreal granite sorry for pinging, but do u see any errors?

unreal granite
#

idk lemme see

#

wait did the bot make that?

dark monolith
#

No

#

But I wanted to check it using the bot

#

But yea.. xD

unreal granite
#

hmm, what does the console say in game, like does it give you a specific line that could be breaking the script?

dark monolith
#

I haven't tried yet

unreal granite
#

try that

dark monolith
#

Hm

river talon
#

h

proven badgerBOT
#
Debug Result

There are 18 errors in this [code](#1134120578031304754 message).
Please read the attached file for the result.

sullen urchin
#

@dark monolith look at this ↑ and you had many mistake in code, I think

dark monolith
#

I already added a lot