Now that we have a basic grasp of enumeration types, we can capture keyboard input using the KeyCode enum.
Update PlayerBehavior with the following code, save, and hit Play:
public class PlayerBehavior : MonoBehaviour
{
public float moveSpeed = 10f;
public float rotateSpeed = 75f;
// 1
public float jumpVelocity = 5f;
private float vInput;
private float hInput;
private Rigidbody _rb;
void Start()
{
_rb = GetComponentRigidbody>();
}
void Update()
{
// ... No changes needed ...
}
void FixedUpdate()
{
// 2
if(Input.GetKeyDown(KeyCode.Space))
{
// 3
_rb.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);
}
// ... No other changes needed ...
}
}
Let's break down this code, as follows:
- We create a new variable to hold the amount of applied jump force we...