- Home /
Change character controller height when Jumping
I'm trying to change the character controller height when jumping which works fine however it doesn't reset to its original height when it touches the ground again after Jumping.
Answer by Unitraxx · Mar 26, 2013 at 04:42 PM
var jumpHeight : float;
var standardHeight : float;
standardHeight = controller.height;
jumpHeight = controller.height/2;
Take these 4 lines out of the update() function and put them together with the other variables that aren't in any function.
Edit : or place these 2 lines
var jumpHeight : float;
var standardHeight : float;
alone outside the function, and place these 2 lines
standardHeight = controller.height;
jumpHeight = controller.height/2;
in start()
Answer by fabio1955 · Mar 26, 2013 at 03:50 PM
You should be more precise. Why don't you attach the code you are using?
Answer by Charles_Gams · Mar 26, 2013 at 04:21 PM
var speed : float = 50.0; var rotateSpeed : float = 3.0; var jumpSpeed : float = 30.0; var doublejumpSpeed: float = 20.0; var gravity : float = 30.0; var target:Transform; var bouncespeed:float = 10.0; private var crouching : boolean = false; var jumpRate = 0.8; private var nextlength = 0.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
var jumpHeight : float;
var standardHeight : float;
crouching = false;
standardHeight = controller.height;
jumpHeight = controller.height/2;
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(controller.isGrounded)
{
controller.height = standardHeight ;
if (Input.GetButtonDown ("Jump"))
{
moveDirection.y= jumpSpeed;
controller.height = jumpHeight;
controller.center.y=0.06;
}
}
if( jumpSpeed == 30.0 && !controller.isGrounded )
{
if(Input.GetButton("Fire2"))
{
var step = bouncespeed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
}
/// This is the code of how it stands
If you jump multiple times, does your controller get smaller and smaller?
I think the problem should be here:
jumpHeight = controller.height/2;
and later (GetButtonDown) you write
controller.height = jumpHeight;
so the height will be shorter eand shorter. I think you should clean up the code for example setting the standardheight in a Start function and declaring it as a private variable..etc...
Yeah, that's exactly what I thought. So the variables should be placed outside the update function as I showed in my answer.
Your answer

Follow this Question
Related Questions
Smoothing out jumping? 1 Answer
I also used to check is grounded raycast and OnCollisionEnter2D but still there was such a problem. 0 Answers
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
Player appears to teleport instead of adding force 2 Answers
Not able to jump when running!Where did I go wrong? 1 Answer