Unity Community


Results 1 to 16 of 16

  1. Location
    Austin, TX
    Posts
    751

    [SOLVED] Disable Char Controller to allow Rigidbody Physics?

    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:  
    1.         rigidbody.velocity = transform.up * 10;
    2.         rigidbody.AddRelativeForce (Vector3.up * 60);

    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:

    Character Controllers are not affected by forces but they can push Rigidbodies by applying forces to them from a script. Usually, all humanoid characters are implemented using Character Controllers.
    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!
    if (Unity) happiness++;


  2. Location
    Austin, TX
    Posts
    751
    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.
    if (Unity) happiness++;


  3. Location
    Convict Hill, TX
    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. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    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.
    I'm wired to the world... that's how I... know... everything...


  5. Posts
    300
    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. Location
    Austin, TX
    Posts
    751
    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.
    if (Unity) happiness++;


  7. Posts
    300
    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. Posts
    300
    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. Posts
    300
    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. Location
    Sarasota, FL
    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.)
    - Brett -
    available for consulting gigs
    CreatedByBrett.com


  11. Posts
    300
    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. Location
    Sarasota, FL
    Posts
    244
    Thanks for the info. It'll be helpful to me in the future. (Like I said, mine was just an educated guess!)
    - Brett -
    available for consulting gigs
    CreatedByBrett.com


  13. Posts
    300
    It was a good guess! I was hoping it would have worked too lol.


  14. Location
    Melbourne
    Posts
    359
    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(CharacterControlle r)); - but it does not disappear - why?

    thanks.


  15. Location
    New Jersey
    Posts
    791
    Quote Originally Posted by reset
    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(CharacterControlle r)); - but it does not disappear - why?

    thanks.
    have you tried something like:

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

    Destroy(myController);


  16. Posts
    300
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •