Sticky Bomb script

I suck at scripting so this took forever to figure out (1 hour-ish), but I did and now I am happy. With this you can make sticky bombs or have a hand be able to dynamically pick up stuff.

make sure the objects that are colliding with eachother have rigid body components. Then put the script on just one of the objects, for instance the sticky bomb, so that when it hits a guy it will stick to him. Cool!

//STICKY BOMB SCRIPT!!!!!!!!!


function OnCollisionEnter(collision : Collision) {
//if the collision is with a rigidbody
if (collision.rigidbody) {
//add a HingeJoint component to the gameobject that has the script
gameObject.AddComponent("HingeJoint");
//set the rigidbody that we collided with as the connected body the hinge connects to
hingeJoint.connectedBody = collision.rigidbody;
}
}

285364–10226–$stickbomb_183.js (429 Bytes)

You could also just make the bomb a parent of the object… Then you could also attach it to objects that are not rigidbody…

Good stuff.

I’d have it do an additional check to find the nearest bone/node of the object it hits and parent it to that. such as throwing a sticky bomb and it hitting a leg. you’d want the stick bomb to move with the leg.