Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Disable Char Controller to allow Rigidbody Physics?

Discussion in 'Editor & General Support' started by Vimalakirti, Feb 8, 2010.

  1. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Dear everyone,

    I have some NPC's that I want to have bounce around after they get hit. They are just capsule-shapes, so there's no rigging. They are controlled with Character controllers, and I think that is keeping them pinned in place when I want them to bounce around. Here is the script that controls the rigidbody part:

    Code (csharp):
    1.         rigidbody.velocity = transform.up * 10;
    2.         rigidbody.AddRelativeForce (Vector3.up * 60);
    3.  
    4.  
    When those lines are active, the NPC bounces around and spins, but doesn't move. The character controller is staying completely still, and the manual states that:

    So the question is how do I have physics take over my NPC when there's a Character Controller attached? Can I disable the Character Controller?

    Thanks for any help!
     
  2. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    I remember now...

    You don't disable the char controller, you replace the entire object with another one with rigidbody, etc, at the same transform, and give it a little force. Then to get the main character back, you re-instantiate it again.
     
  3. GhostDog

    GhostDog

    Joined:
    Nov 11, 2009
    Posts:
    103
    I wonder if it would be easier (or even possible) to suspend components. That way you have both components available and depending on the state of the parent object, toggle the updating of the desired/undesired components. You don't have to worry about instance gymnastics at that point.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't suspend most components, but the rigidbody specifically allows this because it is very useful. The rigidbody has a property called isKinematic which enables non-physics control (ie, kinematic) when it is set to true. You can add a rigidbody to a character and keep it kinematic most of the time but then switch to physics when you want to knock it flying, float it on water or whatever.
     
  5. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    I cannot get this to work. :(

    So as a test, I made a cube, created a rigidBody component, and all is well. It behaves just like how you would expect. If I add a characterController to it, then the physics no longer work anymore, even if isKinematic is turned off.

    Am I missing something here?
     
  6. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    No, Fishy, you understand the situation. Char cont keeps your object from moving around like a rigid body. If you want to use a char controller, then when you want it to bounce, you need to get rid of it and create a prefab object that looks the same but doesn't have the character controller attached. When it's done bouncing, you get rid of that and put your original object back in its place. It's a pain in the botox, but it works.
     
  7. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    :cry: Awww, I was hoping that you could just have both components on the mesh, and just activate or deactivate isKinematic on the rigid body. That would be SOOOO much easier . . . hmmm.
     
  8. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Figured it out, at least a somewhat easier approach. Basically I have a cube with CharacterController and Rigidbody attached.

    When I want the cube to use the Rigidbody, first I get all the attribute values of the CharacterController, delete the CharacterController component, then set the RigidBody isKinematic value to false and tell it to WakeUp().

    When I want control of it again, I set isKinematic to true, and add the CharacterController component back to the cube and reapply the settings.
     
  9. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Does this sound like a valid approach? My goal with this is to implement a sliding / falling aspect to my movement script. Similar to WoW's. I am thinking that just letting the physics handle the sliding when a player is on too steep of an edge will be a lot easier then trying to script my own sliding code (which I have tried, works ok, but not great).
     
  10. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    Hey Fishypants, try just setting the CharacterController.active = false instead of removing it. (That's an educated guess; I haven't tested it at all.)
     
  11. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    Hey Brett, setting active = false results in the cube disappearing. Don't know where it ends up going. The editor says thats an obsolete function and if the goal was to activate / deactivate a component, that we should use 'enabled'. This although results in an error as characterController doesn't have an 'enabled' property. :(

    I really wish this was simpler. I can't believe there isn't a way to just disable the characterController. . .
     
  12. BrettFromLA

    BrettFromLA

    Joined:
    Jan 29, 2009
    Posts:
    244
    Thanks for the info. It'll be helpful to me in the future. (Like I said, mine was just an educated guess!)
     
  13. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    It was a good guess! I was hoping it would have worked too lol.
     
  14. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Hi there

    This is exactly what I want to do.

    I have a NPC/AI character controller that I want to react to my bullets. I have it working - the attached rigidbody becomes "is Kinematic = false" and so my NPC is spinning on the spot when the bullet hits - but it is NOT flying backwards.

    I am trying to remove the Character Component -
    Gameobject.Destroy(GetComponent(CharacterController)); - but it does not disappear - why?

    thanks.
     
  15. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    have you tried something like:

    CharacterController myController = gameObject.GetComponent(typeof(CharacterController));

    Destroy(myController);
     
  16. Fishypants

    Fishypants

    Joined:
    Jan 25, 2009
    Posts:
    444
    You know, I totally got this working, but I cannot find the project file or remember exactly how I did it :?

    I do remember the key to enabling and disabling physics is to call the RigidBody.WakeUp() or RigidBody.Sleep() functions before doing anything else.