using System.Collections; using System.Collections.Generic; using UnityEngine; public class Graple : MonoBehaviour { //Vareables private Vector2 mousePos; //Component refranses public Camera mainCamera; public LineRenderer _lineRenderer; public DistanceJoint2D _distanceJoint; // Start is called before the first frame update void Start() { _distanceJoint.enabled = false; } /// /// Update is called once per frame and calculates if teh grapple should be active, and how the /// grapple should effect the player /// void Update() { //Checking if the player is grappleing if (Input.GetKeyDown(KeyCode.Mouse1)) { // Calculating the mouse position in the world space mousePos = (Vector2)mainCamera.ScreenToWorldPoint(Input.mousePosition); // Creating a line from the mouse position to the player's position _lineRenderer.SetPosition(0, mousePos); _lineRenderer.SetPosition(1, transform.position); _distanceJoint.connectedAnchor = mousePos; //Enableing the grapple _distanceJoint.enabled = true; _lineRenderer.enabled = true; //Checking if the player has stoped grapling } else if (Input.GetKeyUp(KeyCode.Mouse1)) { //Disabling the graple _distanceJoint.enabled = false; _lineRenderer.enabled = false; } // Checking if the grapple is still active if (_distanceJoint.enabled) { // Updating the line to for the current player position _lineRenderer.SetPosition(1, transform.position); } } }