Jump higher when running

Hey guys.

I have a problem with this. When im just walking my “hero” jumping normal so Code for this is looking like this:

			if (Input.GetButtonDown ("Jump")){
					moveDirection.y = 7.0;
			}

This working fine but… when my character is running then i see veeery big glitch. I know what problem it is but i dont know how can i reaper it. Can you help me?

Code is here.

	Screen.lockCursor = true;
	
	var speed : float = 6.0;
	var runningSpeed : float = 10.0;
	//var jumpSpeed : float = 8.0;
	var gravity : float = 20.0;
	var rotateSpeed : float = 10.0;

	private var moveDirection : Vector3 = Vector3.zero;

	function Update() {
		var controller : CharacterController = GetComponent(CharacterController);
		transform.Rotate(0, Input.GetAxis ("Mouse X") * rotateSpeed, 0);
		if (controller.isGrounded) {
			// We are grounded, so recalculate
			// move direction directly from axes
			
			moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
			                        Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
							
			if (Input.GetButtonDown ("Jump")){
				 (Input.GetButtonDown ("Run")){    // <---- I wanna check it if my char is running or no. This is not working...
					moveDirection.y = 7.0;
				}
			}
				
		
		}
			if (Input.GetButton ("Run")){                 // <-------- Here i have my running "code". Working good but when i jumping then my char is flying somewhere, maybe he have enough of me.
				
				moveDirection *=runningSpeed;
					
					if (Input.GetButtonDown ("Jump")) 
						moveDirection.y = 10.0;
			}
			

		// Apply gravity
		moveDirection.y -= gravity * Time.deltaTime;
		
		// Move the controller
		controller.Move(moveDirection * Time.deltaTime);
	}

Help me guys.

A couple issues:

if (Input.GetButtonDown ("Jump")){
                 (Input.GetButtonDown ("Run")){    // <---- I wanna check it if my char is running or no. This is not working...
                    moveDirection.y = 7.0;
                }
            }

You need a second ‘if’ in order to check running. Then, this code will only jump if the player is moving - if that is what you want, then great.

Your bigger issue though, is lower down. If the player is NOT grounded, and running, you multiply the movement vector by running speed every frame, causing it to fly away.

Without altering your code too much, the easiest fix i see is to put the second run block INSIDE of the grounded check, and then adjust like so:

This makes you jump higher when running, only lets you jump when on the ground, and prevents you from flying off.

if (Input.GetButton ("Run")){            
        moveDirection *=runningSpeed;                    
}

 if (Input.GetButtonDown ("Jump")){                        
        if (Input.GetButtonDown ("Run")){    
                    moveDirection.y = 7.0;
         else   moveDirection.y = 10.0;
}