#Entity Knockback in AOE

1 messages · Page 1 of 1 (latest)

final plume
#

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 "^~^

vital depotBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

final plume
#

Here's what I've got so far to show for this :')

hoary briar
#

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())
final plume
#

Okay, that helps a lot ^~^ I was having difficulty following for a sec, but seeing it scripted out really puts it into perspective lol

hoary briar
#

I went with 4 cause that should be about the max distance a mob can be from the player

final plume
#

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

hoary briar
#

something like 4.2 would be the hypotenuse on a 3x3 triangle

final plume
#

omg I can finally use the pythagorean theorem

#

It's been years

hoary briar
#

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

final plume
#

Good to know konk Lemme toy around with it a bit; no doubt this can get complex and fun~

hoary briar
#

oh even better, instead of addMotion..... use .knockback(strength, x, z) bahaha

#

so your strength would be what we were putting in scale

final plume
#

I forgot about .knockback ">.>

#

@me always jumping to the most complicated means before realizing the simpler method smh

hoary briar
#

that adds in the bonus effect of it checking knockback resistance for you

final plume
#

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.

hoary briar
#

which comes back to the distanceToEntity

final plume
#

Yupp ^~^

#

Thank you~!