- Home /
How do I make and object fall through the floor after a specific time?
When I kill the enemy NPC, I instantiate a ragdoll gameObject with this script attached. I need the ragdoll to fall through the ground after a second, then destroy itself a second later. I have made a new layer, called "Nocollide." It is layer 10, and in the matrix is set to not collide with any other layers. After it instantiates, I look in the inspector, and it does change the layer to Nocollide, but doesn't fall through the floor. When I walk into it, I collide with it. If I make the prefab's layer to Nocollide before I press play, it falls through. I don't understand why it still collides. Please help. Thank you in advance!
Here is my script:
void Start ()
{
Destroy(gameObject, 2);
StartCoroutine(FadeAway());
}
IEnumerator FadeAway() {
yield return new WaitForSeconds(1f);
gameObject.layer = 10;
}
Answer by cdrandin · Jul 06, 2013 at 04:38 AM
You could just implement your own simply little timer. Assuming this script to be in each enemy NPC. Give it a function name Destroy. which does... what you want, but you can do it simpler. Why not remove or disable the physics component on your object after 1 sec. This will still achieve what you want, but not have to deal with layers. Also, instead of destroying you can simply disable the object and re-enable it again when you need the rag doll. If the current rag doll is being used then duplicate it. The thing to note here is Destroying GameObjects or Unity's Destroy method can be costly if you are doing it pretty frequently.
Hope this helps.
Edit: Also instead of using WaitForSeconds just making your own timer. I say this because it is expensive as well.
float timer = 0.0f;
if( timer >= 1.0f )
{
// then do something
timer = 0.0f;
}
timer += Time.deltaTime;
I will give that a try in the morning and let you know how it goes. I actually never thought of doing a timer that way before. Great stuff. The script, however is attached to the ragdoll, which is a separate gameObject that is instantiated when the enemy itself has 0 health and is destroyed. I will edit my post to clarify that. Also, I am still very puzzled as to why it doesn't fall through when it clearly changes layer, and the Nocollide layer clearly doesn't collide with anything. Anyways, thanks a lot for the reply!
Answer by robertbu · Jul 06, 2013 at 06:27 AM
You can use Invoke() to cause something to happen in the future. You can cause something to fall through the floor by turning off its collider. Destroy has an optional second parameter for how far in the future the object should be destoryed. Putting the three together:
#pragma strict
function Start() {
Invoke("FallThrough", 2.0);
}
function FallThrough() {
collider.enabled = false;
Destroy(gameObject, 1.0);
}
This code causes the object to start falling through the floor two seconds after it is created and then it is destroyed one second after that.
Thanks for the reply! I like to use C#, but I tried your JS, and I had the same problem. I have now figured it out, though. $$anonymous$$y problem was that I had to change the layer (or in your case disable the collider) of each child, since on a ragdoll, each limb has a separate collider. Again, thanks!
Answer by badguy0325 · Jul 07, 2013 at 01:24 PM
I have now figured out the answer. My problem was that I had to change the layer of each child, since on a ragdoll, each limb has a separate collider.
Your answer
Follow this Question
Related Questions
How would I go about setting an exclusive collision, i.e. floor that only reacts to the player? 1 Answer
Why is my ragdoll reacting differently depending on the framerate? 2 Answers
Ragdoll driven game 1 Answer
Fall Damage OnCollisionEnter Script Help. 1 Answer
How can I stop a rigidbody (Ragdoll) character from colliding with the Character Controller capsule? 1 Answer