Question by
RealGamesss · Jun 18, 2020 at 05:56 PM ·
2dclickgrappling gun
2D grappling hook responding problems
hi, i am working on grappling hook with raycast2d. I have a script, but when I try it, I always have to click a few times before it starts to respond. Don't know what to do with it?
public class GrapGun : MonoBehaviour { public Transform firePoint; public LayerMask ropeLayerMask;
public float distance = 90.0f;
LineRenderer line;
DistanceJoint2D rope;
bool checker = true;
void Start()
{
rope = gameObject.AddComponent<DistanceJoint2D>();
line = GetComponent<LineRenderer>();
rope.enabled = false;
line.enabled = false;
}
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0) && playerGrapGun.activeSelf == true && ui.PauseMenu.activeSelf == false && checker == true)
{
RaycastHit2D hit = Physics2D.Raycast(firePoint.position, lookDirection, distance, ropeLayerMask);
if (hit.collider != null)
{
checker = false;
SetRope(hit);
}
}
else if (Input.GetMouseButtonDown(0) && checker == false)
{
checker = true;
DestroyRope();
}
}
void SetRope(RaycastHit2D hit)
{
GetComponent<PlayerMovement>().enabled = false;
rope.enabled = true;
rope.connectedAnchor = hit.point;
rope.enableCollision = true;
line.enabled = true;
line.SetPosition(1, hit.point);
}
void DestroyRope()
{
GetComponent<PlayerMovement>().enabled = true;
rope.enabled = false;
line.enabled = false;
}
}
Comment