- Home /
Question by
Gamerdev6 · Jul 12, 2020 at 10:09 AM ·
grappling gun
i have made a grappling gun but it does not what to show the line or stop when i have shot it
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);
}
}
Comment
Your answer
Follow this Question
Related Questions
Grappling hook springjoint 0 Answers
Grappling Hook in 2.5d Game 0 Answers
Line End Point move with object 1 Answer
Why does the line not render? 0 Answers