- Home /
How do you make a grappling hook?
After playing the Attack on Titan tribute game, I became interested in the grappling hook, and the way the the physics behind it works. Is there a simple way to make something like a grappling, perhaps in scripting, or through any means possible? I have tried using a spring joint, but I have no idea how I could launch something to grapple out into. Basically, what I intend to do, is create a gameobject the launches out from the grappling hook "gun", latches on to a collider, and creates a spring joint, other kind of joint, or simple something that would pull you towards it.
Not much to go on but look into adding force, there are plenty of tutorials on bullets and such like where you instantiate an object and send it flying off into the distance.
Ok , Ill tell you this much , I have the entire aot script , Except I have no clue how the code works and I also figured that the rope is a part of the animated character sadly I still have no clue of how the code works But I have the entire codes , Camera , character , Hook , Everything , I attatched it to a gameobject and sense they used the character animated few animation errors occur , I am trying to understand the code , But Its difficult it is not black on white , and Im not a c# user that makes it more diffficult for me to understand , either way the answer above with the code has actually a bit of this , And If you start with his code youre on the right path , The Biggest mistery is figuring out the code , I will eventually and then I will let you know After I understand the entire code , but then again I have it , And theres hope :)
Answer by The Red Mohawk · Feb 22, 2015 at 09:33 PM
In c#, you could 'shoot' your grappling hook gameObject by giving it a rigidbody and use this script within Monobehaviour, attached to the hook.
private bool inAir = false;
private HingeJoint grabHinge;
public int speed.
//To shoot your hook, call this method:
void GrapplingShot(){
rigidbody.velocity = new Vector3 (x, y, z) * speed;
inAir = true;
//This is the direction your hook moves multiplied by speed.
}
void OnCollisionEnter (Collision col) {
if (inAir = true) {
rigidbody.velocity = 0;
inAir = false;
grabHinge = gameObject.AddComponent <HingeJoint>();
grabHinge.connectedBody = col.rigidbody;
//This stops the hook once it collides with something, and creates a HingeJoint to the object it collided with.
}
}
This should get you started. I'm not going to code the whole grappling hook for you.
Hello, quick question; when I use this script, I get an error (well, three) that say x, y and z don't exist in the current context. Obviously just writing x, y and z isn't correct, however I don't know what is supposed to go there. Little help?
He was offering logic not so much code, we have no idea what your actual x, y, z will be. Basically he is saying you need to have a vector3 that will force your rigidbody in the direction of your grappled target. That Vector3 gets multiplied by your speed.
So let's say you had a method that returns a Vector3 calculated by the direction from your player to the grappled target, it is called 'GetDirection()' or something...
basically change that line to
rigidbody.velocity = GetDirection() * speed;
and GetDirection() would have all the logic inside it for giving you this value.
hello i have problem with that one can you help me? void GrapplingShot(){ it's saids unexpected symbol 'void'
sounds like one of your previous lines may be missing a semicolon.
Answer by J0hn4n · Sep 15, 2017 at 04:44 AM
@Datester35 I think a simple solution its just raycast to where will going your hook. if hits addforce to that vector else do nothing.
It says Assets\Grapplinghook.cs(3,19): error CS1001: Identifier expected how do i fix this?