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][1] 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 ();
}
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 ();
grabHinge.connectedBody = col.rigidbody;
//This stops the hook once it collides with something, and creates a HingeJoint to the object it collided with.
}
}
}
[1]: http://answers.unity3d.com/questions/907266/how-do-you-make-a-grappling-hook-1.html
↧