# how can i make double jump need help

hi i need help in making a 3rd person charter do double jump this is the code i got for the charter control

``````    private var jumpSpeed:float = 15.0;
private var gravity:float = 20.0;
private var runSpeed:float = 10.0;
private var walkSpeed:float = 5.0;
private var rotateSpeed:float = 150.0;

private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = true;
private var moveStatus:String = "idle";

function Update ()
{
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));

// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}

moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;

moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";

// Jump!
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;

}

// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(2)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}

if(Input.GetMouseButton(2) || Input.GetMouseButton(0))
Screen.lockCursor = true;
else
Screen.lockCursor = false;

// Toggle walking/running with the T key
if(Input.GetKeyDown("t"))
isWalking = !isWalking;

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

//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}

@script RequireComponent(CharacterController)

``````

and this is the animation code i got right now

``````    animation.wrapMode = WrapMode.Loop;

animation["jump"].wrapMode = WrapMode.Clamp;
animation["shoot"].wrapMode = WrapMode.Clamp;
animation["grab"].wrapMode = WrapMode.Clamp;

animation["idle"].layer = -1;
animation["run"].layer = -1;

animation.Stop();

function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
else

if (Input.GetButtonDown ("Jump"))

if (Input.GetButtonDown ("grab"))

if (Input.GetButtonDown ("Fire1"))

}

``````

also if you can make it that the player has latest 3 seconds to press jump again before you cant press it and its reset if you can that will be great

Two things:

1) The reason you can't double jump right now is because jumping is currently inside an `if( grounded )` check. Which he isn't if he's not on the ground. Pull that out of the if statement and do something else to determine whether or not they can jump (like have a counter that resets whenever they're on the ground).

2) For the animation, you'll probably have to do a `.Rewind` when you double jump so that the animation starts over.

here you go use what you need to get it to work, worked for me using the physics engine but same theory should still apply.

var power = 25.0;
var drag = 2.0;
var jump : float = 10;
var doublejump = 0;
private var grounded = false;

function Update ()
{
// Only apply forces if the ball is on the floor

``````	// Calculate the direction in which we want to roll the ball
// * It is relative to the camera
// * we remove the y component because we really want to apply forces only on the 2D plane
var forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;

// Scale the force by the users input and apply it to the rigidbody
var forwardForce = forward * Input.GetAxis("playerY") * power;

var right = Camera.main.transform.TransformDirection(Vector3.right);
forward.y = 0;
right = right.normalized;

// Scale the force by the users input and apply it to the rigidbody
var rightForce = right * Input.GetAxis("playerX") * power;

var jump = 5;

if(Input.GetAxis("jump") && grounded == true)
{
rigidbody.velocity.y += jump;
doublejump += 1;
}

if(Input.GetKeyDown("space") && doublejump == 2)
{
rigidbody.velocity.y += 10;
doublejump = 0;
}

if(Input.GetAxis("flip world"))
{
rigidbody.velocity.y += 5 * Time.deltaTime;
}

// Apply drag only if the user is not pressing any keys
if (Mathf.Approximately (Input.GetAxis("playerX"), 0) && Mathf.Approximately(Input.GetAxis("playerY"), 0))
rigidbody.drag = drag;

if(grounded)
{
rigidbody.drag = 0;

}
else
rigidbody.drag = 0;
// Every frame set grounded to false. OnCollisionStay will enable it again if
// the rigidbody collides with anything
grounded = false;
``````

}

function OnCollisionStay() {
grounded = true;
}

Couldn't you just add a boolean such as isJumping and in the if statement that checks if you hit the jump key set it to true then add another if statement below that to check if isJumping is true. If it is add you additional jump height in there if you hit the Jump button again?

I did something similar but can't remember off hand how I went about it. I turned the Wall Jump from the Lerpz platformer tutorial into a double jump instead.

when i try to use the .Rewind its says that is not a member of the animation state unity engine

character motor CharacterController, doble jump, need not rigbody, you can modify and use

``````    var rapides : float = 6.0;
var gravedad : float = 20.0;
var velocidadSalto : float = 8.0;
var velocidadSalto2 : float = 8.0;
var esperarSalto : boolean;
var esperarSalto2 : boolean;
var dobleSalto : int = 0;
var dobleSalto2 : int = 0;

private var moveDirection : Vector3 = Vector3.zero;

//undap
function Update()
{
if( Input.GetKeyDown(KeyCode.Space) && esperarSalto == false)
{
Jump();
}
var controller : CharacterController = GetComponent(CharacterController);
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 *= rapides;

if (Input.GetButton ("Jump")&& esperarSalto2 == false) {
Jump2();
}
}

// Apply gravity

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

function Jump()
{
if (dobleSalto <= 1)
{
jumpTimer();
}
}

function jumpTimer()
{
if (Input.GetButton ("Jump"))
{
dobleSalto ++;
}

if (dobleSalto > 1)
{
dobleSalto = 0;
esperarSalto = true;
yield WaitForSeconds (1.8);
esperarSalto = false;
}
}
//exra
function Jump2()
{
if (dobleSalto2 <= 1)
{
jumpTimer2();
}
}

function jumpTimer2()
{
if (Input.GetButton ("Jump"))
{
dobleSalto2 ++;
}

if (dobleSalto2 > 1)
{
dobleSalto2 = 0;
esperarSalto2 = true;
yield WaitForSeconds (1.5);
esperarSalto2 = false;
}
}
``````