Help with my double jump script

I am trying to get my character to jump in mid air either left right or just straight up depending on which key I am pressing and also need it to ignore my current velocity however I have not been able to achieve this. I have tried everything from making a doublejump function with else if statements to consulting ancient gypsy fortune tellers but have not been able to get my desired result. Heres my script:

var speed : float = 4.0;
var jump : float = 10.0;
var dJump : float = 10.0;
var gravity : float = 20.0;
var dJumpV : float = -4.0;
var dJumpV0 : float = 4.0;

private var canDJump : boolean = false;
private var velocity : Vector3 = Vector3.zero;

var controller : CharacterController;

function Update() 
{
	
	if (!controller.isGrounded && canDJump && Input.GetKey("a") 

``|| !controller.isGrounded && canDJump && Input.GetKey(“left”))
{
ldJump();
}

	if (!controller.isGrounded && canDJump && Input.GetKey("d") 

``|| !controller.isGrounded && canDJump && Input.GetKey(“right”))
{
rdJump();
}

	if (!controller.isGrounded && canDJump && Input.GetButtonDown("Jump"))
	{
		velocity.y = dJump;
		canDJump = false;
	}
	
	if (controller.isGrounded)
	
	{
		velocity = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
		velocity = transform.TransformDirection (velocity);
		velocity *= speed;
			
		if (Input.GetButtonDown ("Jump"))
			{
				velocity.y = jump;
				canDJump = true;
			}
	}
	
	velocity.y -= gravity * Time.deltaTime;
	controller.Move (velocity * Time.deltaTime);
}	

function ldJump()
{
	if (Input.GetButtonDown ("Jump"))
			{
				velocity.y = jump;
				velocity.x = dJumpV;
			}
}

function rdJump()
{
	if (Input.GetButtonDown ("Jump"))
			{
				velocity.y = jump;
				velocity.x = dJumpV0;
			}
}

Heya,

I think you have complicated the double jump a bit too much. You can use the same approachas you used in your normal jump to figure out the Horizontal movement.

Try this:


#pragma strict

var speed : float = 4.0;
var jump : float = 10.0;
var dJump : float = 10.0;
var gravity : float = 20.0;
var dJumpV : float = 4.0;
private var canDJump : boolean = false;
private var velocity : Vector3 = Vector3.zero;
var controller : CharacterController;

function Update() 
{
	
    if (!controller.isGrounded && canDJump && Input.GetButtonDown ("Jump"))
    {
    	velocity = GetHorizontalMovementDirection();
        velocity *= dJumpV;
        velocity.y = dJump;
        canDJump = false;
    }
 
    if (controller.isGrounded)
    {
        velocity = GetHorizontalMovementDirection();
        velocity *= speed;
        if (Input.GetButtonDown ("Jump"))
        {
            velocity.y = jump;
            canDJump = true;
        }
    }
    velocity.y -= gravity * Time.deltaTime;
    controller.Move (velocity * Time.deltaTime);
}

function GetHorizontalMovementDirection()
{
	var direction : Vector3 =  Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
    return transform.TransformDirection (direction);
}