How would I make a grappling hook?

Started by afk on

Topic category: Help with modding (Java Edition)

Last seen on 02:32, 29. Apr 2024
Joined Mar 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How would I make a grappling hook?

In the latest official April Fools' update, there's an item called the 'Lashing Potato' that works as a grappling hook. When the player right clicks, it shoots out a projectile (not affected by gravity), and if that projectile hits a block, it'll start pulling the player towards the projectile until the player right clicks again to let go.

This visually also has a texture that stretches in a straight line from the player to the projectile. Here's an example of how that looks; https://imgur.com/a/snaJAUh

Now, I have no clue if any of this is possible in MCreator at all; but would anything like this be possible? Preferably including the rope/vine that visually connects to both the player and the projectile/hook. Any help is appreciated.

Last seen on 13:54, 28. Apr 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It's possible! Just, from…
Mon, 04/08/2024 - 11:23

It's possible! Just, from personal experience, very annoying to do properly!

The grappling hook is essentially just a spring, which means it works on spring physics. The game saves the position the hook is attatched to, and then adjusts the player's position based on the tension between the two points, which increases based on distance. Which sounds simple, but involves a fair bit of math. But the basic idea is that you need to save a position as player-lifetime number variables, and then have a logic variable to determine whether the player is attatched to something. Then every update tick, you adjust the player's velocity accordingly.

The tricky part is actually the visual effect. I couldn't figure out how to do an elongated texture like the potato, or the guardian beam, but I was able to make chain links and a hook; I essentially just made them non-collidable entities that constantly set their position to various points along the vector between the player and the attatchment point.

This obviously won't work like an actual rope, and won't take block collisions into account, but should otherwise work pretty effectively. My method also doesn't use an actual projectile, it just instantly attatches to the location you're targeting. (It's possible to use a real projectile, I just found this method to be more satisfying.) 

Here's a link to some of the procedures I used, if you're interested. Best of luck!