Topic category: Help with Minecraft modding (Java Edition)
Hey all, I am making an ability that makes it so when a key bind is pressed, all mobs nearby (in like a 3 block radius) get blown back away from the player depending on where they are relative to the player's position (for example, if mobs are fully surrounding the player they will all get blown back away in a circle formation). I have the key bind made, I just can't figure out how to make the procedure to push mobs away correctly.
I've tried the 'explode at' block but when setting it to a higher explosion power to do more knockback, it ends up doing way more damage than I would like and most of the time just kills the mobs instead. I would like the use the 'override motion vector' block if possible as I feel like it has the best control when fine-tuning exactly how strong the push is.
Figured this out with a slightly janky method, here is the code for anyone who may be interested in doing the same thing.
This is done by forcing all the nearby mobs to look at you, and then launches them in the opposite direction they are looking, and while this does work, I wouldn't mind still accepting a solution that doesn't involve snapping a mobs neck to look at you to if someone knows how.
Late to seeing this, but:
1. Get the mob position (x1, y1, z1).
2. Get the player position (x2,y2,z2).
3. Subtract: mob position minus player position (x1 - x2, y2 - y1, z2 - z1). This gives a vector that points from the player, toward the mob; or, thinking about it another way, it points from the mob, away from the player. Call this vector (x3,y3,z3).
4. It's probably a good idea to normalize this vector. Doing so is annoying, but important. If you don't normalize, then the farther away the mob is, the stronger the push will be... which makes it go even farther, which makes it push even stronger, etc.
5. To normalize:
a) Square each component of the vector: px = x3^2, py = y3^2, pz = z3^2.
b) Add up each component: p = px + py + pz.
c) Take the square root: r = p^(1/2).
d) Go back to the vector you started, and divide each component: x4 = x3/r, y4 = y3/r, z4 = z3/r.
e) You now have a unit vector (x4,y4,z4).
4. Use (x4,y4,z4) to push the mobs, or override their velocity with this vector. If you want the push/velocity to be stronger, multiply each component by some speed factor: (x4*s, y4*s, z4*s).
Hope that helps, let me know if you have any questions.
does this make the mobs not break their neck
@dr.panasaurus correct, mobs should NOT break their neck with my method