Skip to main content
2 of 2
added 49 characters in body

Help with 3D Double Jumping in Unity

I'm having some trouble with vertical movement. I'm trying to add double-jumping but none of the scenarios online seem to be working, and I can only ever manage to get a single jump. Here's my code:

using UnityEngine;
using System.Collections;

public class Moving : MonoBehaviour {
//public float speed;
CharacterController controller;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public bool candoublejump = true;
public int jumptime = 0;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
    controller  = GetComponent<CharacterController>();
}

void FixedUpdate() {
    if (controller.isGrounded) {
        moveDirection = new Vector3 (Input.GetAxis ("Horizontal") * 2000, 0, Input.GetAxis ("Vertical") * 2000);
        moveDirection = transform.TransformDirection (moveDirection);
        moveDirection *= speed;
    }
    if (Input.GetButton ("Jump")) {
        if (controller.isGrounded) {
            moveDirection.y = 0;
            moveDirection.y = jumpSpeed;
            candoublejump = true;
        } else {
            if (candoublejump) {
                candoublejump = false;
                moveDirection.y = jumpSpeed;
            }
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
}

Is there something wrong with my keyboard maybe? My theory is that it's checking for the double jump in the split second after you did the first one, which isn't helpful because you're still holding the jump button.