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.

3 Answers

3

You should be more precise. Why don’t you attach the code you are using?

This should be a comment, not an answer.

This is the code of how it stands

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?

Yes the character controller does get smaller

See my answer (and accept it, if it works)

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.

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

Thanks alot !!