I've been writing a script for an item's right click function to knock back all entities within a certain radius of the player. So far I have a 3x3 bounding box for my aoe using AABB, and I'm capable of targeting each entity within that area, as I currently have it set to deal 1 damage for testing. However, I've been unable to figure out how to add knockback to each entity based on how close they are to the player. I've tried using hurtMarked, but I'm assuming that only affects player knockback. And I've had a little bit of success with addMotion, but I can't quite figure out an equation that would push the entities further the closer they are to the center of the aoe, instead I've only had success doing the opposite by simply subtracting the entity's coord's by the player's. If anyone has experience with something like this, some assistance would be much appreciated "^~^
#Entity Knockback in AOE
1 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
Here's what I've got so far to show for this :')
you can do some fun easy things with vec3s https://lexxie.dev/forge/1.19.2/net/minecraft/world/phys/Vec3.html
player/targetEntity.position() will give you their vec3
can do .distanceTo and .vectorTo and then .scale based on the dist
although there's also entity.distanceToEntity(entity) as well
like something like
targetEntity.attack(1)
let distance = event.player.distanceToEntity(targetEntity)
let vec = event.player.postion().vectorTo(targetEntity.position())
let motion = vec.scale(4-distance)
targetEntity.addMotion(motion.x(), motion.y(), motion.z())
Okay, that helps a lot ^~^ I was having difficulty following for a sec, but seeing it scripted out really puts it into perspective lol
I went with 4 cause that should be about the max distance a mob can be from the player
The website you sent legit fried my brain for a sec; I didn't even know where to start :') Though, I'll definitely bookmark that for reference when I've got my wits about me
something like 4.2 would be the hypotenuse on a 3x3 triangle
and if that's too big of a knockback you could always do .scale((4-distance)/4) to keep it a 0-1 float
that gives you something to play with though
Good to know
Lemme toy around with it a bit; no doubt this can get complex and fun~
oh even better, instead of addMotion..... use .knockback(strength, x, z) bahaha
so your strength would be what we were putting in scale
I forgot about .knockback ">.>
@me always jumping to the most complicated means before realizing the simpler method smh
that adds in the bonus effect of it checking knockback resistance for you
Simply adding "targetEntity.knockback(1, (event.player.x - targetEntity.x), (event.player.z - targetEntity.z));" will deal knockback in relation to where you are comparatively, which solves my problem once I scale the knockback strength.
which comes back to the distanceToEntity