I tried to implement the code you provided here (I put here the whole script. The part you provided is at the bottom):
extends KinematicBody
export var speed : float = 20
export var acceleration : float = 50
export var air_acceleration : float = 5
export var gravity : float = 0.98
export var max_terminal_velocity : float = 54
export var jump_power : float = 20
export(float, 0.1, 1) var mouse_sensitivity : float = 0.3
export(float, -90, 0) var min_pitch : float = -90
export(float, 0, 90) var max_pitch : float = 90
var velocity : Vector3
var y_velocity : float
onready var camera_pivot = $camerapivot
onready var camera = $camerapivot/cameraboom/Camera
onready var animation_tree = $AnimationTree
onready var animation_mode = animation_tree.get("parameters/playback")
func ready():
Input.setmousemode(Input.MOUSEMODECAPTURED)
animationtree.active = true
func process(delta):
if Input.isactionjustpressed("uicancel"):
Input.setmousemode(Input.MOUSEMODE_VISIBLE)
func input(event):
if event is InputEventMouseMotion:
rotationdegrees.y -= event.relative.x * mousesensitivity
camerapivot.rotationdegrees.x -= event.relative.y * mousesensitivity
camerapivot.rotationdegrees.x = clamp(camerapivot.rotationdegrees.x, minpitch, maxpitch)
func physicsprocess(delta):
handle_movement(delta)
func handle_movement(delta):
var direction = Vector3()
if Input.is_action_pressed("forward"):
direction += transform.basis.z
elif Input.is_action_pressed("backward"):
direction -= transform.basis.z
if Input.is_action_pressed("left"):
null
elif Input.is_action_pressed("right"):
null
direction = direction.normalized()
var accel = acceleration if is_on_floor() else air_acceleration
velocity = velocity.linear_interpolate(direction * speed, accel * delta)
if is_on_floor():
y_velocity = -0.01
else:
y_velocity = clamp(y_velocity - gravity, -max_terminal_velocity, max_terminal_velocity)
if Input.is_action_just_pressed("jump") and is_on_floor():
y_velocity = jump_power
animation_mode.travel("AIR")
velocity.y = y_velocity
velocity = move_and_slide(velocity, Vector3.UP)
func _unhandled_input(event):
var is_walk_playing := false # write this outside of procees
if Input.is_action_pressed("forward") or Input.is_action_pressed("backward"):
if not is_walk_playing:
animation_mode.travel("WALK")
is_walk_playing = true
if Input.is_action_just_released("forward") or Input.is_action_just_released("backward"):
if is_walk_playing:
animation_mode.travel("IDLE")
is_walk_playing = false
Honestly, I'm very clueless on this topic. I've only worked with the state machine once and I only really understand how to use it for 2D Top-Down games (e.g. Zelda-style games) through Heartbeast's RPG tutorial.
It also might be a problem with my State Machine. Say, do you know how to use Blendspace 1Ds and animations in the state machine node? I'm pretty sure the Blendspace 1Ds can help with the transition from walk to sprint but when I tried it, it didn't work.
This is how my state machine is currently put together:
https://drive.google.com/file/d/1yV-Kb2KWEMPcqOLcR_NAYt9bpIxe2KSb/view?usp=sharing