Enabling movement while on air

I’m using slightly altered script from reference “CharacterController.Move” as my 2d platformer movement script, but I cannot get the player to be able to move horizontally while on air. Being able to do this is crucial for the game.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {

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 *= speed;

if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

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

Try this. All you have to do is move the part where it calculates the moveDirection out of the if(controller.isGrounded) check.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {

var controller : CharacterController = GetComponent(CharacterController);

moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (controller.isGrounded) {

if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

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

Thank you for attempting to help, however it doesn’t work. When I change the script to one suggested, it makes gravity almost non-existant, so it’s variable needs to be extremely high for it to work. At the same time the jump becomes very small, it’s hard to even notice, and when I tweak “jumpspeed” it will jump higher, but the jump becomes as fast as teleport.
If I only understood script better I wouldn’t need to ask everyone else do work for me. :slight_smile:

Hmm… I remember there being a modified script that did exactly as you wanted, let me see if I can find it, I’ll post back with the link in a sec.

EDIT: Ok here it is, it’s a enhanced fpswalker script that gives you a bunch of different things that you probably won’t use, but it also gives you aircontrol (moving while in air). FPSWalkerEnhanced

Thanks again Astrauk! However, I really don’t like to use code that I cannot totally understand as it makes building around it very problematic. Here is my current code, that makes me able to move while on ground and on air, BUT I cannot jump properly! The Jump is a very small nudge, like something would be intercepting the jump? If I set “jumpSpeed” higher, the jump becomes instant teleport into the air.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
var controller : CharacterController = GetComponent(CharacterController);

// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButtonDown("Jump")  controller.isGrounded) {
moveDirection.y = jumpSpeed;
}


// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

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