This question was
closed Nov 24, 2020 at 04:12 AM by
CTLB.
Question by
CTLB · Nov 23, 2020 at 11:55 AM ·
grappling gun
How to attach grappling hook to a specific tag
Hello, I found a grappling hook code on the internet and I wrote it, but I have a problem, I want it to stick to a certain ground or more precisely, I want it to stick to a tag. my english is not very good, sorry
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class hook : MonoBehaviour { private Vector3 _mousepos; private Camera _camera;
private bool _check;
private DistanceJoint2D _distancejoint;
private LineRenderer _linerenderer;
private Vector3 _temppos;
private void Start()
{
_camera = Camera.main;
_distancejoint = GetComponent<DistanceJoint2D>();
_linerenderer = GetComponent<LineRenderer>();
_distancejoint.enabled = true;
_check = true;
_linerenderer.positionCount = 0;
}
private void Update()
{
GetMousePos();
if(Input.GetMouseButtonDown(0) && _check)
{
_distancejoint.enabled = true;
_distancejoint.connectedAnchor = _mousepos;
_linerenderer.positionCount = 2;
_temppos = _mousepos;
_check = false;
}else if (Input.GetMouseButtonUp(0))
{
_distancejoint.enabled = false;
_check = true;
_linerenderer.positionCount = 0;
}
Drawline();
}
private void Drawline()
{
if (_linerenderer.positionCount <= 0)return;
_linerenderer.SetPosition(0, transform.position);
_linerenderer.SetPosition(1, _temppos);
}
private void GetMousePos()
{
_mousepos = _camera.ScreenToWorldPoint(Input.mousePosition);
}
}
Comment