Changing direction in the Air

Hi,

I am trying to make a 2d character Controller with the code below:
For the most part it works:
The only issue I am having is : when I am jumping and in the Air, if I hit the left arrow or the right arrow it does not respond it keeps moving in the direction before the jump.

So example I am running right and jumped if I hit the left it keeps moving right in the air

Any suggestions ,Thanks

var speed = 8.0;
var jumpSpeed = 15.0;
var gravity = 20.0;
var rotateSpeed = 3.0;
private var grounded : boolean = false;
private var moveDirection = Vector3.zero;

static var hurt:boolean=false;

private var shield : GameObject;
static var freezetime:boolean=false;

var ouchsound : AudioClip;






function FixedUpdate() {

transform.position.z=Mathf.Clamp(transform.position.z, 0, 0);
var controller : CharacterController = GetComponent(CharacterController);

//transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);


var forward = transform.TransformDirection(Vector3.forward);

var curSpeed = speed * Input.GetAxis ("Horizontal");

//controller.SimpleMove(forward * curSpeed);


  
  
  
  if (Input.GetKey (KeyCode.RightArrow)){

transform.rotation = Quaternion.Euler(0, 90, 0);

controller.SimpleMove(forward * curSpeed);
}
  else if (Input.GetKey (KeyCode.LeftArrow)){
controller.SimpleMove(-forward * curSpeed);
transform.rotation = Quaternion.Euler(0, -90, 0);
}




if (grounded) {

if (Input.GetButton("Jump")) {

moveDirection.y = jumpSpeed;

}

}


moveDirection.y -= gravity * Time.deltaTime;


var flags = controller.Move(moveDirection * Time.deltaTime);

grounded = (flags & CollisionFlags.CollidedBelow) != 0;

//if(controller.collisionFlags == CollisionFlags.Below){


/////////////////////////
}

SimpleMove takes care of gravity, and isn’t compatible with the usual jump code - you should use Move instead. I simplified the script and changed to Move:

var speed = 8.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rotateSpeed = 3.0;
private var grounded : boolean = false;
private var moveDirection = Vector3.zero;

static var hurt:boolean=false;

private var shield : GameObject;
static var freezetime:boolean=false;

var ouchsound : AudioClip;

function FixedUpdate() {

  transform.position.z = 0; // <- it's much easier to zero z this way!
  var controller : CharacterController = GetComponent(CharacterController);
  var curSpeed = speed * Input.GetAxis ("Horizontal");
  if (curSpeed > 0) transform.rotation = Quaternion.Euler(0, 90, 0);
  if (curSpeed < 0) transform.rotation = Quaternion.Euler(0,-90, 0);
  moveDirection.x = curSpeed;
  if (controller.isGrounded){ // if character is grounded...
    moveDirection.y = 0; // vertical velocity = 0...
    if (Input.GetButton("Jump")){ // but if jumped pressed...
      moveDirection.y = jumpSpeed; // vertical velocity = jumpSpeed
    }
  }
  moveDirection.y -= gravity * Time.deltaTime; // apply gravity
  controller.Move(moveDirection * Time.deltaTime); // move
}

I’ve not tested this code - let me know if you have any problem.