How would I detect the player walking in the standard movement script?

Godot Version

4.1.1

Question

So I am using the standard CharacterBody3d movement script for my player.
I was wondering how I could set a walking variable to true whenever I start moving.
The player movement looks like this:

        var input_dir = Input.get_vector("right", "left", "back", "forward")
		direction = lerp(direction, (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta*lerp_speed)
		if direction:
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)
			velocity.z = move_toward(velocity.z, 0, SPEED)

		move_and_slide()

Looks pretty good, except wouldn’t you do equivalent when checking on the first one, not you wouldn’t use less than because the player could move backwards.

Actually there already is a variable that tells you if the player is walking or not.

if direction: Here you check if he should move or not.
If you don’t press any of your input directions this will be a Vector3(0,0,0), meaning no direction, so not moving.

Or if you prefer you could create a seperate variable outside any functions and update it inside your movement function:

var is_walking : bool



		if direction:
            is_walking = true
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		else:
            is_walking = false
			velocity.x = move_toward(velocity.x, 0, SPEED)
			velocity.z = move_toward(velocity.z, 0, SPEED)

Also you could check the velocity if you want to know if he is moving even without having any player input, for example falling or sliding down ramps.