[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:

		rigidbody.velocity = transform.up * 10;
		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:

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!

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.

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.

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 cannot get this to work. :frowning:

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?

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.

: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.

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.

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).

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.)

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. :frowning:

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

Thanks for the info. It’ll be helpful to me in the future. (Like I said, mine was just an educated guess!)

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

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.

have you tried something like:

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

Destroy(myController);

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.