Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Hey there! First off, really nice game! The controls take a while to get used to but once you're familiar with them, it's really fun. Especially like the idea of the little orb showing you when you are able to grapple again. :)

That being said, I have a somewhat random question. I am a second semester Game Design student and am currently working on a 2D space game with a grappling hook mechanic (for my exam). Up until now, it has been a massive pain to implement such a mechanic for me and to get it to work properly. I've already spent a lot of time looking for the right solution in various forums and tutorials, without much success.

That's why I'm writing you. Your grappling mechanic looks like it's using a raycast to determine where it can be shot. Is that correct? If so, would you mind if I took a look at the code, to understand how you did it? Not to steal your code, but to finally understand it and get on the right track (hopefully :D)

Have a good week & friendly regards!

Lauritz


PS: Just in case you're curious what the game I am working on looks like, you can find it on my page by the name "Asteroid Dancer" :)

Hey there, Lauritz. Thank you so much for checking out my game, I appreciate the feedback. 

Regarding the grappling mechanic, I'm actually utilizing the physics system in Unity rather than raycasts. The code is a little messy but I'll add some links to the grappling system on pastebin anyway. I'll try to explain what I'm doing as that may be a little more helpful than digging through my spaghetti code.

The grappling hook system consists of 2 main components, the "GrapplingHook" (I sort of think of it as the gun) and the "Grapple" (the projectile fired by the grappling hook). You could certainly do the "Grapple" part with a raycast instead of shooting out an actual physics projectile. When the grappling hook is fired the grapple projectile gets shot out and has a GameObject variable for storing what it is attached to initially set to null. When the grapple collides with something it checks to see if it is in the "grapplable" layer specified in the GrapplingHook script, and if so it sets its attachedTo variable equal to whatever it has hit. Otherwise, if it has hit something not grappleable or goes beyond a maximum range an UnGrapple() method is called to reset the grappling hook.

At this point we can fire the grappling hook and tell if the grapple has hit anything. Now I messed with a distance joint attached to the player object to get the physics behavior I wanted whenever the grapple actually did hit something. The details of this are in the Grapple.cs OnCollisionEnter2D() method. On the DistanceJoint2D I was sure to check Max Distance Only so that there isn't an unnatural feeling pole vault behavior where the grappling hook cannot reduce it's length.

Pastebin Links:

GrapplingHook.cs: https://pastebin.com/Dy88kMMG

Grapple.cs: https://pastebin.com/f393s7Cb

(1 edit)

Hope this helps, let me know if you have any other questions

Hey there, thank you very much for the detailed reply! I really appreciate it. I haven't had the chance to fully dig into your code just yet, because of some assignment from other classes. But on the first glance I think I already vaguely understand why the first iterations of my grapple mechanic didn't work out -- which has to do with how the hook is created in the first place and how the layers and collisions work together. In the next few days I will look into that a bit more deeply.

Have a nice sunday and thanks again for your help!