- Home /
Other
How do i make a simple grappling hook?
I want to make a simple grappling hook (right mouse to fire, 1 to reel in, 2 to reel out, C#). I found this: link but the script gives me an error (well, four) saying x, y and z don't exist and int can't be converted to Vector3. I know x, y and z aren't correct but I don't know what goes there. Can someone either (A) Tell me how to fix it or (B) Direct me to a different tutorial, preferably (but not necessary) involving raycasting?
EDIT Ok, I attached the script to a cube, parented it to the player and it does - NOTHING! Cube doesn't move, no "Grappling" effect. That's because I need (A) A script to make it so that however far away the player is from the point of impact, the player stays that far away and (B) The cube needs to actually move. HELP!?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrapplingHook : MonoBehaviour {
private bool inAir = false;
private HingeJoint grabHinge;
public int speed;
Rigidbody rb;
void Awake () {
rb = GetComponent<Rigidbody> ();
}
void Update () {
if (Input.GetButtonDown("Fire2")) {
GrapplingShot ();
}
}
//To shoot your hook, call this method:
void GrapplingShot(){
rb.velocity = this.transform.forward * speed;
inAir = true;
//This is the direction your hook moves multiplied by speed.
}
void OnCollisionEnter (Collision col) {
if (inAir == true) {
rb.velocity = Vector3.zero;
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.
}
}
}
Well, they aren't defined. It sounds like the goal is to fire something in the direction your character is facing? If that's the case, then you could probably do:
void GrapplingShot(){
rigidbody.velocity = this.transform.forward * speed;
inAir = true;
//This is the direction your hook moves multiplied by speed.
}
But, that example is really more pseudo-code than a working example. Very little is declared explicitly, and the ".rigidbody" property has been deprecated.
Taking a guess:
rigidbody.velocity = 0;
swap that for
rigidbody.velocity = Vector3.zero;
Errors are gone! Thanks, i'll let you know how it works.
Follow this Question
Related Questions
How do I solve this error? 2 Answers
Invalid AABB Error on collision :( 1 Answer
Physics.Boxcast not working with mesh collider 1 Answer
How to move a Sphere using physics 0 Answers
Error: NpRigidDynamic::isSleeping: Body must be in a scene. 1 Answer