How I can change the scale of my CharacterConroller in runtime?

Hello everybody. I have my charactercontroller, is a 2D game, but I want to change it scale in runtime, and I see two problems:
a) When I change the localScale, using lerp, if I make it smaller it stand on the air then fall and walk; if I make it grow, grow through the soil, and then fall over the emptiness.
b) I make click over a place and create a targetPoint, but some times the character controller moves to a different place.

I gonna paste my code, maybe you could help me.

  if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,firstPlane.z)) < 0.05f)
	   //scale = Mathf.Lerp(transform.localScale.x,firstPlaneScale.x,Time.fixedDeltaTime * smoothScale);
 else if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,secondPlane.z)) < 0.05f)
      //scale = Mathf.Lerp(transform.localScale.x,secondPlaneScale.x,Time.fixedDeltaTime * smoothScale);
 else if(Vector3.Distance(new Vector3(0,0,targetPosition.z),new Vector3(0,0,thirdPlane.z)) < 0.05f)
	  //scale = Mathf.Lerp(transform.localScale.x,thirdPlaneScale.x,Time.fixedDeltaTime * smoothScale);

This can get to be a bit tricky. I’ve had the same problems when implementing my player crouch. As you scale the character controller you need to also move the center of it so that you don’t get things like floating up into the air or sink into the ground.

I did not animate my CharacterController and instead I simply snap it in 1 frame as I’ve found it drastically decreased the complexity and I had no need for the character controller collision mesh to animate.

I notice in your code you are scaling your entire gameObject, but I think that’s probably not how you want to do it since that would also scale the mesh of the player. You’ll want to grab an instance of the controller with:

CharacterController myController = transform.gameObject.getComponent();

Then you can controller the scale and height with:


The trick is that when you snap the new scale of the controller, you want the new center to be exactly in the same place as your original center. This will avoid floating into the air or falling into the ground. Here is my crouching code if you’re interested:

void Crouch(){
    charController.height -= crouchHeight; //original value was 2, but crouchHeight = 1 -= new Vector3(0,crouchHeight/2, 0); //original center was 0 but now is 1.

void Stand(){
    charController.height += crouchHeight; += new Vector3(0,crouchHeight/2, 0); 

you can also make one game object with a character controller and only modify the character controller height and radius like here:

var controller : CharacterController;
function Start(){
controller = GetComponent(CharacterController);
controller.height = 2.0;
controller.radius = 0.3;

and the graphics stock it in the gameobject with the character controller
and scale it as u wish … i think that works , if not u can add something like a jump for the character controller so like that it won’t gitch trough the soil