here is the code
using UnityEngine;
public class grapple_gun : MonoBehaviour
{
private LineRenderer lr;
public Vector3 GrapplePoint;
public LayerMask whatGrapple;
public Transform guntip, camera, FPSPlayer;
public float maxDistance, minDistance = 100f;
private SpringJoint joint;
void Awake()
{
lr = GetComponent<LineRenderer>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartGrapple();
}
}
void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance))
{
GrapplePoint = hit.point;
joint = FPSPlayer.gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = GrapplePoint;
float distanceFromPoint = Vector3.Distance(FPSPlayer.position, GrapplePoint);
joint.maxDistance = distanceFromPoint * 1f;
joint.minDistance = distanceFromPoint * 0.25f;
joint.spring = 4.5f;
joint.damper = 7f;
joint.massScale = 4.5f;
lr.positionCount = 2;
}
}
void lateUpdate()
{
DrawRope();
}
void DrawRope()
{
if (!joint) return;
lr.SetPosition(index: 0, guntip.position) ;
lr.SetPosition(index: 1, GrapplePoint);
}
void StopGrapple()
{
lr.positionCount = 0;
Destroy (joint);
}
}