- Home /
irrelevantly fixed
Spawn rigidbody on collision
Hi everyone. I'm working on a game where you can blow up basically anything, similar to Red Faction. I made a script that turns a gameObject into a rigidbody when it is collided with another game object. Everything about the script works, except for one thing. Right as the game starts, the gameobject is destroyed and turned into the rigidbody, even though it is not touching anything.
Can someone help me out with this?
var deadreplacement : Rigidbody;
var explosion : Transform;
function Update()
{
deadreplacement = Instantiate(deadreplacement, transform.position, transform.rotation);
}
function OnCollisionEnter(collision : Collision) {
Destroy (gameObject);
Instantiate(explosion, transform.position, Quaternion.identity);
}
(Please be aware that I am really new to scripting, and not that good at it)
Thanks in advance :D
EDIT I just realized the gameobject is being destroyed because the rigidbody is colliding with it -_-
you are new to scripting but it looks like you have the right feel, good luck!
as a rule ... DO NOT instantiate during game play in a video game. ins$$anonymous$$d, you make a "pool" of objects, sit them offscreen, and use them as you see fit.
here's a novel on the topic ! ...
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Answer by crushy · Sep 30, 2012 at 02:32 AM
Your object is probably colliding with the ground or something similar. Have you checked the layer it's in?
I just realized it's being destroyed because the rigibody that is spawns collides with it. Now I just need to find out how to make the rigidbody spawn after it's destroyed...
Follow this Question
Related Questions
Helicopter Crash Collision 0 Answers
Smart camera? 1 Answer
OnCollisionEnter function problem. JS 2 Answers
Can't add Rigidbody component because collision prevents it 2 Answers