GrapplingGun | FEP
Mon Apr 04 2022 20:16:48 GMT+0000 (Coordinated Universal Time)
Saved by @BiscuitTinx #c#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Grappling : MonoBehaviour { private LineRenderer lr; private Vector3 grapplePoint; public LayerMask whatIsGrapple; public Transform gunTip, cam, player; private float maxDistance = 100f; private SpringJoint joint; private void Awake() { lr = GetComponent<LineRenderer>(); } private void Update() { if (Input.GetMouseButtonDown(0)) { StartGrapple(); } else if (Input.GetMouseButtonUp(0)) { StopGrapple(); } } // called after update private void LateUpdate() { DrawRope(); } /// <summary> /// call whenever we want to start a grapple /// </summary> void StartGrapple() { RaycastHit hit; if (Physics.Raycast(cam.position, direction: cam.forward, out hit, maxDistance)) { grapplePoint = hit.point; joint = player.gameObject.AddComponent<SpringJoint>(); joint.autoConfigureConnectedAnchor = false; joint.connectedAnchor = grapplePoint; float distanceFromPoint = Vector3.Distance(a: player.position, b: grapplePoint); // the distance grapple will try to keep from grapple point joint.maxDistance = distanceFromPoint * 0.8f; joint.minDistance = distanceFromPoint * 0.25f; // change the values to make it fit your game joint.spring = 4.5f; joint.damper = 7f; joint.massScale = 4.5f; lr.positionCount = 2; } } /// <summary> /// call whenever we want to stop a grapple /// </summary> void StopGrapple() { lr.positionCount = 0; Destroy(joint); } void DrawRope() { // if not grappling, dont draw rope if (!joint) { return; } lr.SetPosition(index: 0, gunTip.position); lr.SetPosition(index: 1, grapplePoint); } public bool IsGrappling() { return joint != null; } public Vector3 GetGrapplePoint() { return grapplePoint; } private void OnDisable() { StopGrapple(); } }
Comments