- Home /
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);
iTween.ScaleTo(transform.gameObject,firstPlaneScale,10f);
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);
iTween.ScaleTo(transform.gameObject,secondPlaneScale,10f);
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);
iTween.ScaleTo(transform.gameObject,thirdPlaneScale,10f);
Answer by justinl · Sep 28, 2012 at 03:53 AM
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:
myController.height; myController.center;
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 charController.center -= new Vector3(0,crouchHeight/2, 0); //original center was 0 but now is 1. }
void Stand(){ charController.height += crouchHeight; charController.center += new Vector3(0,crouchHeight/2, 0); }
Thanks for your help... I guess I understand what you are explaining, I'm gonna try to do something like that. And by the way, I'm working with sprites, and really I want to make the sprite at the same size than my controller.
No problem. I haven't worked that much with sprites in Unity, but I still feel that you probably wouldn't want to scale the sprite card itself and probably you don't even need to scale the sprite card itself. If you did scale the card, texture mapping could get weird and if you animated the scale ins$$anonymous$$d of snapped it, you'd have to do some pretty complex reverse scaling on your texture so that it didn't look like it started to squish. Just my 2 cents.
Well, but at really I want to re-scale the sprites, for the moment I'm not having problems with the textures, I'm using 2DToolkit to handle the sprites and make animations with them, but I want to move my character not only in X-axis but in Z-axis, that's my actually problem.
Answer by rokyed · Sep 29, 2012 at 07:24 AM
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
I guess I'm getting the trick, Im gonna try to make it work making an object, adding the charactercontroller to it and the scripts, and my animatedSprite like a child of the first gameObject, so I can treat the charactercontroller and the sprite by separate... I'm gonna tell you if works, thanks