- Home /
Grappling hook mechanic not working
Greetings fellow coders! So I have recently started a new first-person game. I have so far followed Brackey's first-person movement tutorial, then went on to make a grappling gun so I can attach and move to "things". This is my code:
using UnityEngine;
public class hook : MonoBehaviour {
public Transform cam;
private RaycastHit hit;
public bool attached = false;
public float momentum;
public float speed;
public float step;
void Start()
{
}
void Update()
{
if (Input.GetButtonDown("Fire1")){
if (Physics.Raycast(cam.position, cam.forward, out hit)){
attached = true;
}
}
if (attached){
momentum += Time.deltaTime * speed;
step = momentum * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, hit.point, step);
}
}
}
for some reason when I try running the scene and "use" the grappling gun I lose the ability to jump and I don't end up grappling to the cube. Not sure what to do so consulting with you fellow experts out there! Thanks in advance ! P.S If you need more code or info I can provide it
Answer by BestPoly · Jan 10, 2020 at 03:56 PM
Hi, I didn't try this code , but I think you should do something like this:
void Update()
{
if (Input.GetButton("Fire1")){
if (Physics.Raycast(cam.position, cam.forward, out hit))
{
transform.position = Vector3.MoveTowards(transform.position,
hit.point, Time.deltaTime * speed);
//if player is near stop go forward
}
}
}
Your answer
Follow this Question
Related Questions
Why does the line not render? 0 Answers
Grappling Hook in 2.5d Game 0 Answers
Line End Point move with object 1 Answer
Need auto aim 0 Answers