I'm using this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrapplingHook : MonoBehaviour {
private bool grappling;
private HingeJoint grabHinge;
RaycastHit hit;
public float range;
GameObject parent;
Vector3 reposition;
GameObject positioning;
GameObject player;
void Awake () {
parent = GameObject.Find ("FirstPersonCharacter");
positioning = GameObject.Find ("Transform");
reposition = positioning.transform.position;
player = GameObject.Find ("FPSController");
}
void Update () {
if (Input.GetButtonDown("Fire2")) {
grappling = true;
GrapplingShot ();
}
if (Input.GetButtonUp ("Fire2")) {
grappling = false;
}
if (grappling == false) {
positioning.GetComponent().line.enabled = false;
transform.position = reposition;
transform.parent = parent.transform;
}
}
//To shoot your hook, call this method:
void GrapplingShot(){
if (grappling == true) {
transform.parent = null;
Physics.Raycast (transform.position, transform.forward, out hit, range);
transform.position = hit.point;
positioning.GetComponent().Rope ();
if (Input.GetKey ("q")) {
player.transform.position = Vector3.MoveTowards (player.transform.position, hit.point, 1);
}
}
}
}
For a grappling hook and this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RopeForHook : MonoBehaviour {
public LineRenderer line;
GameObject hook;
void Awake () {
line = GetComponent ();
hook = GameObject.Find ("Grappling Hook");
}
void Update () {
}
public void Rope () {
line.enabled = true;
line.SetPosition (0, hook.transform.position);
}
}
For the rope. However, when I test it, the hook doesn't move with the player, although it sort of follows the player's rotation. The hook also shoots on a weird diagonal angle to the player, the "reel-in" part does nothing and the rope shoots straight out behind the player. There is obviously something very wrong with my script, and I can't find it after meticulous trial-and-error searching. Please help me fix this!
↧