[SOLVED] Pushing Away All Nearby Mobs

Started by Alec Cature on

Topic category: Help with Minecraft modding (Java Edition)

Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[SOLVED] Pushing Away All Nearby Mobs
Sat, 03/09/2024 - 21:56 (edited)

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.

Edited by Alec Cature on Sat, 03/09/2024 - 21:56
Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Figured this out with a…
Sat, 03/09/2024 - 21:55

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.

Joined Nov 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Late to seeing this, but:1…
Sat, 11/09/2024 - 18:14

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.

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
does this make the mobs not…
Wed, 12/18/2024 - 21:01

does this make the mobs not break their neck

 

Joined Nov 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@dr.panasaurus correct, mobs…
Wed, 12/18/2024 - 21:42

@dr.panasaurus correct, mobs should NOT break their neck with my method