When I enter play mode, VertVelocity is subtracted by gravity every second, as expected. The problem is that I can only jump when VertVelocity is equal to 0. (Which was not expected.) When I move VertVelocity is equal to 0, then with some random decimal value after that. I can only jump when the VertVelocity is 0.random_decimal. How do I change this so I can jump whenever player.isGrounded = true
?
using System.Collections;
using UnityEngine;
public class PlayerJump : MonoBehaviour {
public CharacterController player;
public float VerticalVelocity;
public float Gravity = 14.0f;
public float JumpForce = 10.0f;
void Start ()
{
player = GetComponent<CharacterController> ();
}
void Update () {
if (player.isGrounded) {
VerticalVelocity = -Gravity * Time.deltaTime;
if (Input.GetButtonDown ("Jump")) {
VerticalVelocity = JumpForce;
}
} else {
VerticalVelocity -= Gravity * Time.deltaTime;
}
Vector3 moveVector = new Vector3 (0, VerticalVelocity, 0);
player.Move (moveVector * Time.deltaTime);
}
}