The current script I have set on my NPC causes it to turn into a ragdoll upon collision with any object. How do I tweak the script to instantiate the ragdoll upon collision with one object/prefab.
Here is the script:
var explosion : Transform;
// When a collision happens destroy ourselves
// and spawn an explosion prefab instead
function OnCollisionEnter ()
{
Destroy (gameObject);
var theClonedExplosion : Transform;
theClonedExplosion = Instantiate(explosion,
transform.position, transform.rotation);
}
use the information that can be obtained from the collider :
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html
You could use any identifier you can think of; a name, tag, layer, rigidbody, meshname …
function OnCollisionEnter( other : Collision ) {
if (other.tag == "ThisObj" || other.layer == 8)
{ ..... DoStuff();
EDIT :
I don’t know when exactly you want the gameObject to be destroyed, or by collisions with what objects. So I shall imagine you only want to destroy when you hit an object that is an ‘Enemy’.
First, create a tag and call it Enemy.
http://docs.unity3d.com/Documentation/Components/Tags.html
http://docs.unity3d.com/Documentation/Components/class-TagManager.html
Then change the tag on all the gameObject colliders you want to destroy the object with this script on. Change them to Enemy.
Then have your code as something like this (untested example, but if you have set the tags correctly it should work) :
var explosion : Transform;
// When a collision happens, First spawn an explosion prefab and Then destroy ourselves
function OnCollisionEnter( other : Collision ) {
if (other.tag == "Enemy") {
var theClonedExplosion : Transform;
theClonedExplosion = Instantiate(explosion, transform.position, transform.rotation);
// after everything else is done, now the object and the script are destroyed
Destroy (gameObject);
}
}