Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Sticky Cube

Discussion in 'Physics' started by moontoad, Jan 8, 2015.

  1. moontoad

    moontoad

    Joined:
    Dec 10, 2014
    Posts:
    4
    So I am trying to make a sticky cube that I can pick up.

    It has this script associated with it:

    using UnityEngine; using System.Collections;

    public class StickyObject : MonoBehaviour { FixedJoint joint; void Start () {

    }

    void Update () {
    if (Input.GetKeyDown(KeyCode.Delete)) {
    if (joint != null) {
    Destroy(joint);
    joint = null;
    }
    }
    }
    void OnCollisionEnter(Collision c)
    {
    joint = gameObject.AddComponent<FixedJoint>();
    joint.connectedBody = c.rigidbody;

    }

    }

    When I try to drop the cube it breaks the joint but it is still connected to the rigid body that picked it up, any ideas on how to destroy the joint?