#how do I apply knockback in the movement direction of an entity?
1 messages · Page 1 of 1 (latest)
@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
Ah, thx, and how would I set the entity velocity of the player to 1 in that direction?
what do you want to be 1?
length of a vector?
I think so
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
oh if you want that specifically then somebody has made setvelocity for players using knockback already
i cant remember who or where it was tho
but ig with this setup just do 1 for horizontalStrength and it should work idk
maybe something else idk how the calcs work on knockback tbh
Really?
The question is where
i think i saw it on bedrock oss server
Hmm
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
There are 3 errors in this [code](#1134120578031304754 message):
[36m<repl>.js[0m:[33m3[0m:[33m5[0m - [31merror[0m[30m TS2683: [0m'this' implicitly has type 'any' because it does not have a type annotation.
[7m3[0m this.applyKnockback(vector.x, vector.z, horizontal, vector.y < 0.0 ? 0.5 * vector.y : vector.y);
[7m [0m [31m ~~~~[0m
``````ansi
[36m<repl>.js[0m:[33m6[0m:[33m18[0m - [31merror[0m[30m TS2551: [0mProperty 'setVelocity' does not exist on type 'Player'. Did you mean 'getVelocity'?
[7m6[0m Player.prototype.setVelocity = setVelocity;
[7m [0m [31m ~~~~~~~~~~~[0m
[36m@minecraft/server.d.ts[0m:[33m4956[0m:[33m13[0m
[7m4956[0m getVelocity(): Vector3;
[7m [0m [36m ~~~~~~~~~~~[0m
'getVelocity' is declared here.
``````ansi
[36m<repl>.js[0m:[33m7[0m:[33m18[0m - [31merror[0m[30m TS2551: [0mProperty 'setVelocity' does not exist on type 'Entity'. Did you mean 'getVelocity'?
[7m7[0m Entity.prototype.setVelocity = setVelocity;
[7m [0m [31m ~~~~~~~~~~~[0m
[36m@minecraft/server.d.ts[0m:[33m4956[0m:[33m13[0m
[7m4956[0m getVelocity(): Vector3;
[7m [0m [36m ~~~~~~~~~~~[0m
'getVelocity' is declared here.
do you want it to launch the player based off where they're moving so like W A S D, or like in the direction they're looking at?
->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
entity.setVelocity(Vector.multiply(entity.getVelocity(), 10))
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...
i dont think its about kb
I mean, the title says other wise lol
all i know is that they have different inputs
but they pretty much do the same thing
wait wait wait
im confused now
whats correct?
i thought entity velocity normalized is entity movement vector
@whole bronze @unreal granite
now I'm confused
entity.getVelocity() is for how fast something is traveling I'm pretty sure
which means it'll work for any direction
ya, if u want 1 then just use the velocity
oh w8, ye, its not 1 magnitude
mb
use Vector and normalize it ig
ah, i just use this on entity getVelocity right?
ye, but also direction
when normalize its just direction
huh
wdym, didn't you want it to be based off which direction you're moving in
or an entity
just want to get movement direction
dw
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
Oh, but get velocity also returns speed
true
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?
hmm, what does the console say in game, like does it give you a specific line that could be breaking the script?
I haven't tried yet
try that
Hm
h
There are 18 errors in this [code](#1134120578031304754 message).
Please read the attached file for the result.
@dark monolith look at this ↑ and you had many mistake in code, I think
I already added a lot