Toggle Run

While I was setting up the movement, I decided to make a run/walk toggle and wanted to share:

[SerializeField] private float runSpeed = 5.66f;
[SerializeField] private float walkSpeed = 3f;

private void ProcessControls() {
            if (Input.GetMouseButton(0)) {
                MoveToCursor();
            }
        
            if (Input.GetKeyDown("left shift")) {
                ToggleSprint();
            }
        }

private void ToggleSprint() {
            isSprinting = !isSprinting;
            navMeshAgent.speed = isSprinting ? runSpeed : walkSpeed;
        }

Privacy & Terms