Running mechanic changes speed during jumping

Godot Version



I’m trying to develop a 3d game, and have a very small amount of experience in coding. I understand some logic considerations but don’t necessarily know how to implement what i want. Im beggining to make a 3d platformer.

I followed a tutorial on youtube and have implemented a running mechanic that changes the animation played when jumping, so when running, the jumping animation is a fast jump, and when walking, the jump animation is just a falling animation. My problem here is that when the player is jumping, the animation should be determined from what the player was doing when grounded, and not be able to change. Meaning that if the jump is started while running, the running jump animation should play, and the speed should be set to the running speed, regardless of the run key being pressed, and the same for walking. However what actually happens is if the run key is pressed mid air, the animation and speed change. I tried to fix this using logic and only managed to make it so that the walking jump animation played.

This is the code for my CharacterBody3D

extends CharacterBody3D

var SPEED = 5.0
const JUMP_VELOCITY = 4.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@export_range(0,1) var sens_horizontal = 0.5
@export_range(0,1) var sens_vertical = 0.5
@onready var animation_player = $visuals/Character/AnimationPlayer
@onready var camera_mount = $camera_mount
@onready var neck = $camera_mount/neck
@onready var visuals = $visuals

var move = false
var jump = false
var run = false

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _input(event):
	if event is InputEventMouseMotion:
		camera_mount.rotate_y(deg_to_rad(-event.relative.x * sens_horizontal))
		#visuals.rotate_y(deg_to_rad(event.relative.x * sens_horizontal))
		neck.rotate_x(-deg_to_rad(event.relative.y * sens_vertical))
		var current_rotation = camera_mount.rotation_degrees
		current_rotation = neck.rotation_degrees
		current_rotation.x = clamp(current_rotation.x, -40, 90)
		neck.rotation_degrees = current_rotation
func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta
		if run:'Running_Jump')
		jump = true
		jump = false

	# Handle Jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	var move_direction:= Vector3.ZERO
	move_direction.x = Input.get_action_strength("left") - Input.get_action_strength("right")
	move_direction.z = Input.get_action_strength("forward") -Input.get_action_strength("back") 
	move_direction = move_direction.rotated(Vector3.UP,camera_mount.rotation.y).normalized()
	velocity.x = -move_direction.x * SPEED
	velocity.z = -move_direction.z *SPEED
	velocity = velocity
	if Vector2(-velocity.z,-velocity.x).length() > 0.2:
		var look_direction =Vector2(-velocity.z, -velocity.x)
		visuals.rotation.y = look_direction.angle()
func _process(_delta):

	if Input.is_action_pressed("sprint"):
		run = true
		SPEED = 5
		run = false
		SPEED = 1.3

	if Input.is_action_pressed('forward')||Input.is_action_pressed('back')||Input.is_action_pressed('left')||Input.is_action_pressed('right'):
		move = true
		if !jump:
			if run:'Run')
		move = false
		if !jump:"Idle")