Make A Cube 'Sticky'

Hello,

I want the ability to make a cube 'sticky' so it would stick to walls or other objects.

Example: Maybe this cube is a sticky cam, or C4 or something around that. How would I go about making this cube have gravity (rigidbody) all the time, and then when it comes in contact with a wall, the gravity is killed (causing it to 'stick') -- Unless any of you have another idea?

Better yet, when the user walks up to a wall and presses 'Fire1' the cube is thrown/placed - or at least 'activates' the 'stick'.

Thanks, Ollie

You can dynamically create a fixed joint when the object collides with something, like this:

C#

using UnityEngine;
using System.Collections;

public class StickyObject : MonoBehaviour {
    void OnCollisionEnter(Collision c) {
        var joint = gameObject.AddComponent<FixedJoint>();
        joint.connectedBody = c.rigidbody;
    }
}

or Javascript:

function OnCollisionEnter(c : Collision) {
    var joint = gameObject.AddComponent(FixedJoint);
    joint.connectedBody = c.rigidbody;
}

You'd place the script on to the moving object, and when that object touches any other rigidbody in your scene, it will create a fixed joint that attaches it to that object.

You can try something as simple as this, a 1 touch is a permanent stick. Quick and easy, and definatly not a pro script. It's just a quick fix ;) like using spit for glue

private bool hookedToSomething = false;

void Update()
{
   if (hookedToSomething)
   {
       transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
   }       
}

void OnControllerColliderHit(ControllerColliderHit cch)
{
    //You detected a collision with the object
    hookedToSomething = true;
}

Right,

Its getting there, few problems though:

I have a model of C4, which basically, when the user presses 'e' it Initiates a rigidbody (which is a copy of the C4)

The model script is:

var C4 : Rigidbody;

function Update()
{
    if (Input.GetKeyDown ("e"))
    {
        if (C4) {
        var Throw : Rigidbody = Instantiate(C4, transform.position, transform.rotation);
        }

    }
}

And the Copy (the one that is being thrown) is:

function Start ()
{

    rigidbody.AddRelativeForce (Vector3(1 * 100,-1 * 100,0));

}

function OnCollisionEnter(c : Collision) {
    var joint = gameObject.AddComponent(FixedJoint);
    joint.connectedBody = c.rigidbody;
}

What I would like to do now is for the collider on the C4 copy to ignore the Player Controller and the C4 copy and original and anything with the tag "test"

Hope that makes sense,

Regards, Ollie

I have a game where i do this! Heres the script i used (JS)

function OnCollisionEnter ()
{
 Destroy (rigidbody);
}

attatch that to the projectile.