Search Unity

Unparented Object not falling

Discussion in 'Physics' started by Atiaen, Feb 21, 2019.

  1. Atiaen

    Atiaen

    Joined:
    Jan 19, 2019
    Posts:
    15
    Ok so i have a gun model parented to the hand of my AI so i was able to get the gun to unparent from the hand of the AI when the AI is killed but to get the gun to fall i added both a mesh collider and rigidbody to the model so when i started having weird issues or example the gun starts floating outside the hand of the AI or if i check IsKinematic on the rigidbody when the AI dies it doesn't fall it just gets stuck in the air, i also tired unchecking IsKinematic and freezing the position and rotation but that doesn't work either. So i'm curious to know if there's a fix for this.
    Thanks
    Code (CSharp):
    1. public GameObject Gun;
    2. void OnAIDeath()
    3.         {
    4.    
    5.             Gun.transform.SetParent(null);
    6.        
    7.         }
    That's some part of the code i used
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  3. Atiaen

    Atiaen

    Joined:
    Jan 19, 2019
    Posts:
    15
    Hmm i'll try this out later thanks
    But are there any other things i could try out?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    i'd try setting parent this way also,
    Gun.transform.parent = null;

    and check in hierarchy that it actually works (unparents it)

    need to disable [ ] isKinematic, and disable those freeze positions too, it seems you tried that.
     
  5. Atiaen

    Atiaen

    Joined:
    Jan 19, 2019
    Posts:
    15
  6. Atiaen

    Atiaen

    Joined:
    Jan 19, 2019
    Posts:
    15

    The thing is if I disable isKinematic the Gun starts to move on its own and doesn't aim properly at all
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    did you try disabling after unparent?
     
  8. Atiaen

    Atiaen

    Joined:
    Jan 19, 2019
    Posts:
    15
    Ok i finally got it to work there were two things i did
    1.) I moved the script to the parent model(yes i know i feel like a moron now)
    2.) I did something like this i first made isKinematic true and then i added the following lines of code
    Code (CSharp):
    1. public Gameobject Gun;
    2. public Rigidbody  rig;
    3. void OnAIDeath()
    4. {
    5.       Gun.transform.parent = null;
    6.       rig.isKinematic = false;
    7. }
     
    mgear likes this.