I’m making a simple 2D platformer game and would like to make a grappling hook that the player can use by clicking in a certain game object.
I’ve followed Wabble - Unity tutorials, tutorial (DonHaul Game Dev - Wabble - Unity Tutorials - YouTube) I’ve followed the tutorial and copied the code correctly but it doesn’t seem to work.
Does anyone know how to fix it?
Here’s the code I’m currently using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rope_Placement : MonoBehaviour
{
DistanceJoint2D Joint;
Vector3 targetPos;
RaycastHit2D hit;
public float distance = 10f;
public LayerMask Mask;
// Start is called before the first frame update
void Start()
{
Joint = GetComponent<DistanceJoint2D> ();
Joint.enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z = 0;
hit = Physics2D.Raycast(transform.position, targetPos - transform.position,distance,Mask);
if (hit.collider!=null && hit.collider.gameObject.GetComponent<Rigidbody2D>() !=null)
{
Joint.enabled = true;
Joint.connectedBody = hit.collider.gameObject.GetComponent<Rigidbody2D>();
Joint.distance = Vector2.Distance(transform.position,hit.point);
}
}
if (Input.GetMouseButtonUp(1))
{
Joint.enabled = false;
}
}
}