#State Machine Issue
1 messages · Page 1 of 1 (latest)
extends State
class_name PlayerForward
@export var player : CharacterBody3D
@export var speed = 5
func Enter():
print("Entering PlayerForward state.")
func Exit():
print("Exiting PlayerForward state.")
func Physics_Update(delta: float):
var velocity = player.velocity
velocity.z += -speed
print("Moving Forward")
if Input.is_action_just_released("Forward"):
Transitioned.emit(self,"Idle")
player.move_and_slide()``` This is the script for the player's forward state
extends State
class_name PlayerIdle
@export var player : CharacterBody3D
@export var speed = 5
func Enter():
print("Entering Idle state.")
func Exit():
print("Exiting Idle state.")
func Update(delta: float):
if Input.is_action_just_pressed("Forward"):
Transitioned.emit(self,"Forward")
if Input.is_action_just_pressed("Back"):
Transitioned.emit(self,"Back")
if Input.is_action_just_pressed("Left"):
Transitioned.emit(self,"Left")
if Input.is_action_just_pressed("Right"):
Transitioned.emit(self,"Right")
func Physics_Update(delta: float):
var velocity = player.velocity
velocity.x = move_toward(velocity.x,0,speed)
velocity.z = move_toward(velocity.z,0,speed)
player.move_and_slide()
```and this is for the Idle state
it prints out the Enter, Exit, and Moving Forward texts, but the player doesnt change positions
You have to update the player.velocity property after you've calculated the velocity. So, before calling player.move_and_slide() add this line
player.velocity = velocity
Do this in both scripts.