Rlly cool idea - is it possible to choose the grappling hook direction using button inputs - like arrow keys Also a suggestion that could be cool - character switching mechanic - 4 different characters that can be switched between with different moves each Each character would have different sprites/ abilities and movements Sortof like pyra/mythra from super smash bros ultimate
You definitely could! If you look at our raycast overloads, instead of shooting a ray from the mouse, into the scene.; You could shoot one from origin player, with some maximum distance, in the input direction. Character switching could be really cool! I will add it to the list! :D
@@ThatOneUnityDevI recently tried this but I keep getting stuck on setting the origin - I’ve tried many different methods but am unsure how to go about this? R u able to spare some advice
@@npkrash No problem! So, from what you've said before, you wanted to have the grapple hook use arrow key input. What you will need to do is get a direction relative to the player, based on input. For the raycast, change the origin to be the player's position, and the direction to be the direction you calculated. You would also want to change the input to probably not the mouse. So, overall, not too many changes. lol. I typically don't do this, and don't advise asking others, but seeing as you came back here to ask for advice again, 3 months later, I have taken the liberty of implementing my advice. Here is a pastebin file, with working code, that implements all of the above! I highly suggest taking a good look at it, and understanding what was changed! :) pastebin.com/gDG7Rrfa In this example, I have drawn the input direction in red in the inspector, to show the direction the grapple will shoot. As a side note, I still suggest extending the logic, to implement features that dynamically change the grapple length for a 'better' grapple hook.
I would love to see a bat physic. Even when you change the bat position, it still swings when clicking the button. I just started learning unity and i made the bat swing with pivot point but as soon as i change the bat position, it stops working and starts to teleport
Hey thank you for that ^^ I was wondering if there is a way I could animate that rope so it would make some kind of sine-waved "whip" movement towards the desired ray cast position, The Line Renderer could work with that but I am scared it would be performing poorly when placing so many line segments
Hi there, thanks for watching! A couple of things could be happening here. I would first ensure that the grapple layer mask and the object you want to grapple to are BOTH set to the same thing. For example, the ceiling layer. You may have accidently created a layer, but didn't actually set your object to it. The problem with raycasts is that you can also accidently 'block' the object you're trying to detect. This should be solved with the layer mask, but just for arguments sake, try ensuring you don't have overlapping colliders where you're trying to grapple to. Hopefully this helps! 👌
heya, thanks for the tutorial. i have an already established character controller and everything including guns. I did exactly everything, changed few stuff here and there, no matter what i do i cant make it work. i debugged a bit, it reads the mouse click input, returns null value on "hit", sometimes boxcollider on the objects, does not grapple, but moves the character in a weird way. im a bit confused, any clue what could be the problem?
It sounds like maybe your grapple layers aren't setup correctly, or maybe interference from other scripts. I recommend basically doing what I did just for testing purposes. Have a square with a box collider2d, and rigidbody2d. Then, add just the grapple script. If that works, slowly add each script until you find the culprit. Hopefully it's just as simple as fixing your layers though. 🤞
Very useful video! Thanks so much. I replaced the distance to the distance of the mouse to the cinemachine camera, but it doesnt collide with objects.. Can anyone help?
I figured it out!! I just needed to enable collisions at the distance anchor. Really helpful tutorial though! Keep making good quality videos like this one!!
It's a good challenge to recreate mechanics in an engine you like, I often try and recreate things in Flax. It'll help you learn the engine you like and have a better understanding of the mechanic (imo).
It is possible to do with a different collider such as a composite collider or tilemap collider? I've tried having everything on the same layer but it doesn't seem to work
@@ThatOneUnityDev wow thanks for the fast reply! I figured out the collider but now I'm having trouble changing the speed of the grappling hook. I find that I'm flying up to the ceiling way too fast. any way I could find a fix?
@anikahedden6057 Haha, no problem! I'm glad you got it working! Unfortunately, with this method, we are letting the joint do all the work for us. So when it comes to tweaking things like speed, we don't really have that option! What I would recommend is to move the player to the grapple point via code, then create the joint. That way, you can have much more fine-tuned movement!
Absolutely! In the same if statement we check if there is a valid collider, we would also use something like Vector3.Distance, and compare the players position to hit.point. This would prevent the grapple from working unless the player was close enough to the grapple point!
In this quick example, we are using the joint to move the player. Therefore there is no "speed" variable we can play with. I would suggest manually moving the player to the destination, and only after he reaches there, enabling the joint. By doing it this way, you will be able to control exactly how fast/slow you want him to be.
@@ThatOneUnityDev Hey how would you go around actually moving the character to the point, I tried using vector2.movetowards but it just doesnt move towarsd the character, thanks
@@Itaisss For sure, no problem! By using the raycast that we already have (that is checking if there is a valid grapple point). We can easily grab the exact spot we clicked by using hit.point. From there, we would have a desired position (the spot we clicked). And that's pretty much the hardest part. So with our newly found desired position, we can subtract the vectors of that, and the player - and grab the direction of that desired position. Then it's as simple as moving the player along that direction, with some sort of speed constraint. Then, when he get's however close you like, enable the joint as we were doing before. I know all of that sounds maybe a little complicated explained via text, but if you take it step by step, i'm sure you will get there! :)
Don't forget to leave your suggestions in the comments! :)
I am liking the episodes. Good stuff
Glad you like them! :)
Ayooo!!!!! \o/
Very cool use of the DistanceJoint2D component!!
Next Suggestion:: SPIKE TRAP!
Glad you liked it! :) ... Nice! I will add it to the list! :D
Rlly cool idea - is it possible to choose the grappling hook direction using button inputs - like arrow keys
Also a suggestion that could be cool - character switching mechanic - 4 different characters that can be switched between with different moves each
Each character would have different sprites/ abilities and movements
Sortof like pyra/mythra from super smash bros ultimate
You definitely could! If you look at our raycast overloads, instead of shooting a ray from the mouse, into the scene.; You could shoot one from origin player, with some maximum distance, in the input direction.
Character switching could be really cool! I will add it to the list! :D
@@ThatOneUnityDevI recently tried this but I keep getting stuck on setting the origin - I’ve tried many different methods but am unsure how to go about this? R u able to spare some advice
@@npkrash No problem! So, from what you've said before, you wanted to have the grapple hook use arrow key input. What you will need to do is get a direction relative to the player, based on input. For the raycast, change the origin to be the player's position, and the direction to be the direction you calculated. You would also want to change the input to probably not the mouse. So, overall, not too many changes. lol.
I typically don't do this, and don't advise asking others, but seeing as you came back here to ask for advice again, 3 months later, I have taken the liberty of implementing my advice. Here is a pastebin file, with working code, that implements all of the above! I highly suggest taking a good look at it, and understanding what was changed! :)
pastebin.com/gDG7Rrfa
In this example, I have drawn the input direction in red in the inspector, to show the direction the grapple will shoot. As a side note, I still suggest extending the logic, to implement features that dynamically change the grapple length for a 'better' grapple hook.
I would love to see a bat physic. Even when you change the bat position, it still swings when clicking the button. I just started learning unity and i made the bat swing with pivot point but as soon as i change the bat position, it stops working and starts to teleport
I will add it to the list! :)
Hey thank you for that ^^
I was wondering if there is a way I could animate that rope so it would make some kind of sine-waved "whip" movement towards the desired ray cast position, The Line Renderer could work with that but I am scared it would be performing poorly when placing so many line segments
@dnoldGames Not at all! The line renderer is super lightweight, I wouldn't worry about too many line segments unless you're using over 100! :)
I also added an background but i cant see rope if i add background but when i remove it shows
Must be your sorting layers on the line renderer, and background sprite!
For some reason, this only works if I set the Grapple Layer Mask to default. I set it to anything else and it won't work.
Did I set something wrong?
Hi there, thanks for watching!
A couple of things could be happening here. I would first ensure that the grapple layer mask and the object you want to grapple to are BOTH set to the same thing. For example, the ceiling layer. You may have accidently created a layer, but didn't actually set your object to it.
The problem with raycasts is that you can also accidently 'block' the object you're trying to detect. This should be solved with the layer mask, but just for arguments sake, try ensuring you don't have overlapping colliders where you're trying to grapple to.
Hopefully this helps! 👌
heya, thanks for the tutorial. i have an already established character controller and everything including guns. I did exactly everything, changed few stuff here and there, no matter what i do i cant make it work. i debugged a bit, it reads the mouse click input, returns null value on "hit", sometimes boxcollider on the objects, does not grapple, but moves the character in a weird way. im a bit confused, any clue what could be the problem?
It sounds like maybe your grapple layers aren't setup correctly, or maybe interference from other scripts. I recommend basically doing what I did just for testing purposes. Have a square with a box collider2d, and rigidbody2d. Then, add just the grapple script. If that works, slowly add each script until you find the culprit. Hopefully it's just as simple as fixing your layers though. 🤞
@@ThatOneUnityDev sadly it wasnt the layers, but in the end your tutorial let me think of a completely different mechanic xD thanks man, cheers!
Very useful video! Thanks so much. I replaced the distance to the distance of the mouse to the cinemachine camera, but it doesnt collide with objects.. Can anyone help?
I figured it out!! I just needed to enable collisions at the distance anchor. Really helpful tutorial though! Keep making good quality videos like this one!!
cool can you do it in Godot
I didn't think the channel was called ThatOneGodotDev 😂
It's a good challenge to recreate mechanics in an engine you like, I often try and recreate things in Flax. It'll help you learn the engine you like and have a better understanding of the mechanic (imo).
It is possible to do with a different collider such as a composite collider or tilemap collider? I've tried having everything on the same layer but it doesn't seem to work
@anikahedden6057 Yeah for sure! It should work with any type of collider you can think of. :)
@@ThatOneUnityDev wow thanks for the fast reply! I figured out the collider but now I'm having trouble changing the speed of the grappling hook. I find that I'm flying up to the ceiling way too fast. any way I could find a fix?
@anikahedden6057 Haha, no problem! I'm glad you got it working! Unfortunately, with this method, we are letting the joint do all the work for us. So when it comes to tweaking things like speed, we don't really have that option! What I would recommend is to move the player to the grapple point via code, then create the joint. That way, you can have much more fine-tuned movement!
@@anikahedden6057 What was your fix for the collider? i think I'm currently having the same problem...
Is there a way to add like a max range on the hook?
Absolutely! In the same if statement we check if there is a valid collider, we would also use something like Vector3.Distance, and compare the players position to hit.point. This would prevent the grapple from working unless the player was close enough to the grapple point!
can you help me how to low the speed when i hook, it goes really fast, not naturally.
In this quick example, we are using the joint to move the player. Therefore there is no "speed" variable we can play with. I would suggest manually moving the player to the destination, and only after he reaches there, enabling the joint. By doing it this way, you will be able to control exactly how fast/slow you want him to be.
@@ThatOneUnityDev Hey how would you go around actually moving the character to the point, I tried using vector2.movetowards but it just doesnt move towarsd the character, thanks
@@ThatOneUnityDev Any tips or advice on how to do that?
@@Itaisss For sure, no problem! By using the raycast that we already have (that is checking if there is a valid grapple point). We can easily grab the exact spot we clicked by using hit.point. From there, we would have a desired position (the spot we clicked). And that's pretty much the hardest part.
So with our newly found desired position, we can subtract the vectors of that, and the player - and grab the direction of that desired position. Then it's as simple as moving the player along that direction, with some sort of speed constraint. Then, when he get's however close you like, enable the joint as we were doing before.
I know all of that sounds maybe a little complicated explained via text, but if you take it step by step, i'm sure you will get there! :)
@@ThatOneUnityDev I got it working after a lot of errors 🤣
thank you so much!
yo, you are "Code Monkey"?) your voice is identical :D
Haha, i'm not code monkey, but i'll take it as a compliment! :)
@@ThatOneUnityDev ahaha 🫡🫡🫡