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);
}
}