- Home /
Question by
tylermakesgames1 · Nov 09, 2021 at 01:23 AM ·
unity 2dphysics2dspringjointspringgrappling gun
How do I connect a spring joint 2d to a rigidbody in script?
I am trying to create a simple grappling hook system using a spring joint and I would like the players spring joint to anchor to any rigidbody clicked on and let go when it's not being clicked on
public class GrapplingHook : MonoBehaviour { private SpringJoint2D player; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
public void OnMouseOver()
{
if(Input.GetMouseButtonDown(0))
{
player = GameObject.FindGameObjectsWithTag("Player").GetComponent<SpringJoint2D>();
player.connectedBody = GetComponent<Rigidbody2D>();
}
}
}
Above is my code for any object that I would like the player to be able to grapple onto but I am met with this error message "'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]"
Comment