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.

2 Answers

2

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?

hello i have problem with that one can you help me? void GrapplingShot(){ it's saids unexpected symbol 'void'

@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?