Search Unity

Rigidbody not sleeping on instantiation

Discussion in 'Scripting' started by captain.carl77, Mar 3, 2010.

  1. captain.carl77

    captain.carl77

    Joined:
    Dec 2, 2009
    Posts:
    28
    I've got a letter of the alphabet that is made up of pieces. I Sleep() it and when it wakes up, because the collision spheres of each piece are colliding, it appears to explode as they push out from each other. I like this effect so I'm using it. However, when I make a prefab out of the letter and its scripts, and then try to instantiate it, it explodes on instantiation, even though I use: function Awake(){ rigidbody.Sleep();}. The prefab works fine if I manually add it. Only when I instantiate it, does it not Sleep(). Any ideas of what I'm doing wrong?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Try using the isKinematic flag to enable/disable the collider objects rather than sleeping them.
     
  3. captain.carl77

    captain.carl77

    Joined:
    Dec 2, 2009
    Posts:
    28
    Hmm. That seems to work with the letter not exploding on instantiation. However, now it won't explode when I attempt to set isKinematic = false;

    Here is my code:

    var CollisionSound : AudioClip;

    private var hasPlayed : boolean;

    function OnMouseDown()
    {
    for (var child : Transform in transform)
    {
    child.rigidbody.isKinematic = false;
    }


    if (!hasPlayed)
    {
    audio.PlayOneShot(CollisionSound);
    hasPlayed = true;
    }

    }
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Sorry, forgot to mention... you should wake the rigidbody after setting isKinematic to false again.
     
  5. captain.carl77

    captain.carl77

    Joined:
    Dec 2, 2009
    Posts:
    28
    thanx andeeee. This worked great. Any ideas on why objects with rigidbodies won't instantiate "asleep" in case I run into any similar problems?
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    A rigidbody object usually automatically wakes up when it it contacts with another collider (sleeping just means the motion from the last collision or force is ignored, but physics isn't switched off as such). This contact check apparently isn't made after disabling isKinematic, but I'm not exactly sure why it is designed this way.