- Home /
Grapple Gun?
I've been looking for a script to create a grappling hook on the internet but all of the codes seem a little too complicated. What i want to do is to be able to only click on certain gameobjects that allow the grappling hooks to attatch onto and retract the player to that position. I'm pretty sure I have to use a raycast to see where the grapple hook has to go, and also a line renderer to create the rope for the grapple hook. But i'm really not sure about how to combine these and how to retract the player to the position. I'm a bit of a noob to coding so some description of the code would be really helpful as well.
Thanks In advance
Answer by The-Game-Master · Jun 29, 2015 at 12:47 AM
Although I'm not exactly sure how to do it, I'd imagine it'd have to do with Vector3.MoveTowards(). You could do the following:
Raycast when the mouse is clicked (or however you do it)
Get the gameObject's tag from the raycast.hit information
if the tag is an allowed surface to grapple onto, use Vector3.MoveTowards(transform.position, RaycastHit.transform.position);
Interesting! $$anonymous$$y player also has a constant force added onto him, would this affect the way he moves after grappling? also after he reaches the grappled position would he still be able to continue moving or would he still be stuck at the grapple's final position? But thanks for the answer that actually helps quite a lot :)
No problem! If you wanted the player to be stuck until he hits a key, you could just use an if statement and a boolean. That boolean would be false until the key is pressed, then the boolean would be true. While the boolean is false, it would disable the character controller script, and once it is true, it would re-enable the character controller.
And no, it does not effect the player's movement after the grapple is released. (to release the grapple after the player reaches the spot, use this:)
if(Vector3.Distance(transform.position, RaycastHit.transform.position) >= 1){
if($$anonymous$$ovePlayer == true){
Vector3.$$anonymous$$oveTowards(transform.position, RaycastHit.transform.position, .07f);
}
}
else{
$$anonymous$$ovePlayer = false;
}
That way, the grapple would only move the player if they're more than 1 unit away from the raycast hit. Then, it would set the bool to false, and it would stop moving you.
Wow thats super helpful! I'll try it out soon and let you know how it goes :)
You're welcome! Sidenote: While grappling, you may want to disable the player's movement script, that way they can't fall, and they can't move while in the air.
Your answer
Follow this Question
Related Questions
Raycast Tag of Hit GameObject 1 Answer
Raycast to Terrain (Conditional Statements) 1 Answer
raycast to object, load wrong script!? 2 Answers
Help with LineRenderer 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers