- Home /
Question by
Lava_Key · Mar 01, 2020 at 06:02 PM ·
2d-platformergrappling gun
2D grappling hook problems
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 (https://www.youtube.com/channel/UCuXkaW-PS6zmJ5zO4FbiiXQ) 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;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity 2D Grapple hook problems 1 Answer
2D grappling hook attaching to moving objects 0 Answers
Grappling Hook/Arm in 2D 0 Answers
2D Player Movement 0 Answers
Why the Unity 3D Tutorials don't work with Unity 3? 2 Answers