Move horizontally while in air

Hello people, I am creating a side scroller game and I would like to allow the player to move horizontally while they are in air (after jumping). However, when I try to do this now, it is locked on the y-axis. What should I do to this code?

var moveSpeed = 5.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;

function FixedUpdate () {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= moveSpeed;

    
    if (Input.GetButton ("Jump")) {    
    moveDirection.y = jumpSpeed;
    }
    }
    
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}

@script RequireComponent(CharacterController)

See the part where it says “if controller.isGrounded”? In there is where moveDirection is set. You need to move even if the controller is not grounded, so just move some of that out of the if statement.

moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= moveSpeed;
    
    if (controller.isGrounded) {
        if (Input.GetButton ("Jump")) {    
            moveDirection.y = jumpSpeed;
        }
    }

I modified it to the code above, but now the movement is very slow, and the character starts off floating in air.

Try this:

var moveSpeed = 100.0;
var jumpSpeed = 200.0;
var gravity = 350.0;
private var moveDirection = Vector3.zero;
var verticalSpeed = 0.0;

function FixedUpdate () {
	var controller : CharacterController = GetComponent(CharacterController);
	moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
	moveDirection = transform.TransformDirection(moveDirection);
	
	if (controller.isGrounded) 
	{
		verticalSpeed = 0.0;
			
		// Jump		
		if (Input.GetButton ("Jump")) 
		{
			verticalSpeed = jumpSpeed;
		}
	}
	verticalSpeed -= gravity * Time.deltaTime;
	
	var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0);
	movement *= Time.deltaTime;

    // Move the controller
    controller.Move(movement * Time.deltaTime);
}

@script RequireComponent(CharacterController)

I’m not sure if this is the best way to do it, but its kind of how I did my player controller, and it seemed to work good for what I needed. It’s similar to what you were doing, but instead of directly modifying the moveDirection’s y component, I created another variable for the vertical speed, let gravity change it, and used it in another Vector3 for the actual movement variable used in controller.Move. You might need to tinker with some of the values, but hopefully this should help some.

This code gives me the same exact response as the one before, even with tweaked variables.

I found it faster to just turn gravity off and set the y position for the duration I wanted the player floating and then turned gravity back on when I wanted the player to descend.