Search Unity

2D Sticky Object

Discussion in 'Scripting' started by artistshc, Dec 7, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi there. I would like to have a game object that everything sticks to....but this is a 2D game, and some of the suggestions on the forum don't seem to work with a 2D game. Any suggestions? This is what people use for 3D games.... I

    Code (CSharp):
    1. void OnCollisionEnter(Collision c) {
    2.         var joint = gameObject.AddComponent<FixedJoint>();
    3.         joint.connectedBody = c.rigidbody;
    4.     }
    And this doesn't work (I'm assuming because their is no FixedJoint2D).

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D c) {
    2.         var joint = gameObject.AddComponent<FixedJoint>();
    3.         joint.connectedBody = c.gameObject.GetComponent<Rigidbody2D> ();
    4.     }
    Any suggestions would be great. I don't want to use parent/child.

    Thanks!
    Rachel
     
  2. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    There is a Fixed Joint 2d.