F-S Machine, character body velocity setup

Godot Version

4.1.3 stable

Question

When setting up a state machine, I don’t quite understand how to access character speed. Just call

@export var player_body: CharacterBody2D
speed = player_body.velocity.move_toward(...) # e.g.

directly or somehow differently?

I have Player class, related to CharacterBody2D node:

extends CharacterBody2D
class_name Player

const SPEED = 80.0
const ACCELERATION = SPEED / 10.0
var input_direction: Vector2

I’d like to setup physics specifically in state scripts.

State Machine code
Idle code
Player code

isnt it in the Player code, it has const SPEED? it’s a constant of 80.0 speed. you get the player “node” by @export var player_body: CharacterBody2D, then if you just want the speed, just access player_body.SPEED, no? which still doesnt make sense, because it’s a constant 80 speed, did you mean velocity?
if it’s velocity, then just player_body.velocity will get the speed value

1 Like

in fact SPEED is the maximum speed:

velocity = velocity.move_toward(vector() * SPEED, ACCELERATION)

I wrote that I want to set up exactly the velocity and my code example logic unfortunately conflicts with the next lines (but there are differences: speed and SPEED).

So I already figured out that CharacterBody2D.velocity is proper, and now trying to call velocity through node name_class Player, but once again I’m trying to figure out why the script doesn’t see the AnimationTree:
image
image

@onready var animations = AnimationTree
you forgot the $ sign

The joke is that everything is the same as I did before:
image
image

put the var animation = tree_animation.get(“parameters/playback”) to @onready

That was originally, I desided to separate it to try

image

here because the $AnimationTree is not ready, the previous code you shown, you take the $AnimationTree variable reference when it’s ready, that’s correct, then just use it on ready of the animation to get the playbacks

Also all properties is default (except Playback but I just tried to cure it by this way) and I commented _ready() function:

image

image

I don’t understood you clearly, but I tried to put it into _ready() function (it’s stupid because @onready already do it). I really don’t get what you mean (maybe because it’s not my mother language), can you clarify please if it’s no trouble?

@onready var tree_animation= $AnimationTree
@onready var animation = tree_animation.get(“parameters/playback”)

i meant this

try replace the playback code to this above

Unchanged. I noticed that this may be the clue:

image

I looked for some AnimationTree definers or binds, but nothing.

1 Like

now i see why, you are trying to access animation tree from Idle’s script node

the $AnimationTree Works only if your script is attached on Player, to do this and easily without typing $../../AnimationTree you can just drag and drop the AnimationTree Node to the code script, it will generate the NodePath for you

2 Likes

Thanks for help and patience!

2 Likes