Question by
thomasfrai97 · Apr 07, 2020 at 07:35 AM ·
2dspringjointgrappling gun
grapple gun via SpringJoint2D,Grapple gun via SpringJoint2D
Kinda stuck on my script. The player cant do anything because he hooks himself.
Any ideas?
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private SpringJoint2D grappler;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
grappler = gameObject.AddComponent<SpringJoint2D>();
grappler.autoConfigureDistance = false;
grappler.enabled = false;
grappler.dampingRatio = 1f;
grappler.frequency = 1.2f;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartGrapple();
}
if (Input.GetMouseButtonUp(0))
{
StopGrapple();
}
}
private void StartGrapple()
{
if (grappler.enabled) return;
Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
RaycastHit2D hit = Physics2D.Raycast(transform.position, dir, 50);
if (!hit.collider) return;
grappler.connectedBody = hit.collider.attachedRigidbody;
grappler.distance = hit.distance * 0.6f;
grappler.enabled = true;
}
private void StopGrapple()
{
if (!grappler.enabled) return;
grappler.enabled = false;
}
}
Comment