Turn CharacterController off and on

I’d like to make my character hop on a wagon or in a vehicle and sit there nice and still. To get him to move with the vehicle I make the character a child of the vehicle.

Unfortunately the CharacterController causes him to slip off anyway, even when not updating the CharacterController with moves.

Inactivating the GameObject isn’t an option, I need it active for other reasons. The active attribute of the component won’t do, it inactivates the GameObject.

Removing and adding the CharacterController isn’t desirable, I have to muck with saving its settings.

I’d like to see if there’s a clean approach before applying a band-aid for something that should be there in the first place.

I’ll be surprised if the bright folks at Unity didn’t provide a way to turn the CharacterController off and on!

First off, make sure you got a main script you can use to control the sub-script from. For example the vehicle script, if you got one, can tell the CharacterController script to disable itself, and then tell it to enable again once leaving the vehicle.

This is done by making sure your vehicle-script, or main script, can communicate with the CharacterController script.

Add this in the vehicle/main script:

var characterController : CharacterController;
function Awake() {
   characterController = GetComponent("CharacterController");
}

Now you can access public functions in CharacterController. Create a function like this within the CharacterController script:

public function ScriptEnabled(bool : boolean) {
   if (bool == false) {
      enabled = false;
   } else {
      enabled = true;
   }
}

This now lets you disable and enable the script as you wish by calling it from your vehicle/main script, using this syntax:
characterController.ScriptEnabled(bool)
(Where “bool” is either “true” or “false” of course.)

Hope this solved your case.