Hello, can someone help.
I've combined DaniDevs Grappling gun script with Brackeys GunSwitching script and I'm having an issue, If I switch to a different gun in the middle of grappling, the line stays active and I can still swing, when I switch back to the grapple gun the line reappears.
The script in question:
using System.Collections;
using UnityEngine;
public class GrapplingGun : MonoBehaviour
{
private LineRenderer lr;
private Vector3 grapplePoint;
public LayerMask whatIsGrappleable;
public Transform gunTip, camera, player;
private float maxDistance = 100f;
private SpringJoint joint;
public GameObject GrappleGun;
public bool canGrapple;
void Awake()
{
lr = GetComponent();
}
void Update()
{
if (GrappleGun.activeInHierarchy == false)
{
canGrapple = false;
}
else
{
canGrapple = true;
}
if (Input.GetMouseButtonDown(0) && canGrapple)
{
StartGrapple();
}
else if (Input.GetMouseButtonUp(0) && !canGrapple)
{
StopGrapple();
}
}
//Called after Update
void LateUpdate()
{
DrawRope();
}
///
/// Call whenever we want to start a grapple
///
void StartGrapple()
{
RaycastHit hit;
if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable))
{
grapplePoint = hit.point;
joint = player.gameObject.AddComponent();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplePoint;
float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);
//The distance grapple will try to keep from grapple point.
joint.maxDistance = distanceFromPoint * 0.45f;
joint.minDistance = distanceFromPoint * 0.25f;
//Adjust these values to fit your game.
joint.spring = 7f;
joint.damper = 4f;
joint.massScale = 2f;
lr.positionCount = 2;
currentGrapplePosition = gunTip.position;
}
}
///
/// Call whenever we want to stop a grapple
///
public void StopGrapple()
{
lr.positionCount = 0;
Destroy(joint);
}
private Vector3 currentGrapplePosition;
void DrawRope()
{
//If not grappling, don't draw rope
if (!joint) return;
currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 8f);
lr.SetPosition(0, gunTip.position);
lr.SetPosition(1, currentGrapplePosition);
}
public bool IsGrappling()
{
return joint != null;
}
public Vector3 GetGrapplePoint()
{
return grapplePoint;
}
}
Can someone tell me how I can turn the line off as soon as I switch weapons
↧