- Home /
2D grappling hook attaching to moving objects
hey so I am trying to make a game with a grappling hook (thats also a frogs tongue its not important but I wanted to mention it)... i want to have it so you can pull objects with the grapple hook however we have a few major bugs I cant suss out... the main one is that it doesnt connect to objects, it only connects to where the object is so you cant connect to moving objects. if someone can help solve this problem it would be much appreciated.
//what can the rope hit
public LayerMask ropeLayerMask;
//whats the max Distance at which the rope can hit
public float MaxDistance_Tongue = 90.0f;
public float MaxDistance_Pull = 25.0f;
//render the Line and the rope object
LineRenderer Line;
DistanceJoint2D Tongue;
SpringJoint2D Pull;
//what direction are you firing the rope
Vector2 lookDirection;
//check if it hit anything if its true then no if its false then it hit
bool DidItHit = true;
public static bool isSwinging;
//this is what you swing off and a list of where the rope positions are (basically where should the anchor be to account for wrapping)
public GameObject Anchor;
void Update()
{
//the lines position is 0
Line.SetPosition(0, transform.position);
//where is the line between the mouse and the camera
lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
Debug.DrawLine(transform.position, lookDirection);
//if you press doun on the mouse and did it hit is true (meaning its able to hit somthing)
if (Input.GetMouseButtonDown(0) && DidItHit == true)
{
//raycast out a hit at the look direction, based on the max distance and if its able to touch the layer masl
RaycastHit2D hit = Physics2D.Raycast(transform.position, lookDirection, MaxDistance_Tongue, ropeLayerMask);
//if it hit and it isnt null set the rope
if (hit.collider != null)
{
DidItHit = false;
SetRope(hit);
}
}
//if you do that but its false it means its already out and you need to remove the rope
else if (Input.GetKeyDown(KeyCode.F) && DidItHit == false)
{
DidItHit = true;
DestroyRope();
}
if (Input.GetMouseButtonDown(1) && DidItHit == true)
{
//raycast out a hit at the look direction, based on the max distance and if its able to touch the layer masl
RaycastHit2D hit = Physics2D.Raycast(transform.position, lookDirection, MaxDistance_Pull, ropeLayerMask);
//if it hit and it isnt null set the rope
if (hit.collider != null)
{
DidItHit = false;
SetPull(hit);
}
}
else if (Input.GetMouseButtonUp(1) && DidItHit == false)
{
DidItHit = true;
DestroyRope();
}
}
//enable the tongue, set the anchor to the hit point and enable the line at the hit point
void SetRope(RaycastHit2D hit)
{
isSwinging = true;
Tongue.enabled = true;
Line.enabled = true;
Tongue.connectedAnchor = hit.point;
Line.SetPosition(1, hit.point);
}
i know what i need to do. i need to have it attach to a gameobject but I dont know how to get this to function and I am stumped
Your answer
Follow this Question
Related Questions
Knockback effect in 2D. 1 Answer
How do I move a character WITHOUT acceleration/deceleration? 1 Answer
Jumping from special jump orb 1 Answer
Problem with Falling Platform Script 3 Answers