Trouble Double Jumping

Hi,
Im trying to get a game object to Double Jump and am having trouble getting it to work.

public class Jump : MonoBehaviour
{
[SerializeField]//atribute
private float _speed = 5.0f;
[SerializeField]//atribute
private float _gravity = 1.0f;
private CharacterController _controller;
[SerializeField]//atribute
private float _jumpHeight = 15.0f;
private float _yVelocity;
[SerializeField]
private bool _canDoubleJump = false;

// Start is called before the first frame update
void Start()
{
_controller = GetComponent();
}

// Update is called once per frame
void Update()
{
float HorizontalInput = Input.GetAxis(“Horizontal”);
Vector3 direction = new Vector3(HorizontalInput, 0, 0);
Vector3 velocity = direction * _speed;

if (_controller.isGrounded == true)
{
//if hit space key
//jump
if(Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
_canDoubleJump = true;
}
}
else
{
if (Input.GetKeyDown(KeyCode.Space))
{
if(_canDoubleJump == true)
{
_yVelocity -= _jumpHeight ;
_canDoubleJump = false;
}

}
_yVelocity -= _gravity;
}

velocity.y = _yVelocity; //cache velocity
_controller.Move(velocity * Time.deltaTime);

}
}

Fixed,Changed _yVelocity -= _gravity to _yVelocity = _gravity

Welcome! If you post a code snippet, PLEASE USE CODE TAGS:

How to use code tags: Using code tags properly

You can edit your post above for others to benefit from.

If you wanna fiddle with another approach to jumping, double-jumping, coyote jumping, etc., check out my treatment of it here:

The above project (proximity_buttons) is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

1 Like

Thank You
Jeff