Reputation: 31
Having issues implementing animation to my script
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
#having a hard time adding animation tree
I tried adding Animation player to the script. and point it to the animation tree yet i cant make it play the animations according to the directions
Upvotes: 3
Views: 5113
Reputation: 40315
In principle, you don't need to interact with the AnimationTree
form your code at all.
What you need is to set an AnimationPlayer
with the animations you want, then set an AnimationTree
, make sure that:
anim_player
points to your AnimationPlayer
(it points to nothing by default).advance_expression_base_node
points to something useful. I recommend setting it to your CharacterBody2D
(it points to the AnimationTree
itself by default).tree_root
is not empty. I recommend setting to a new AnimationNodeStateMachine
.active
is set to true
.And, of course, you need to make the actual animation tree in the AnimationTree panel that appears on the bottom of the editor.
If you are using an AnimationNodeStateMachine
as root, you can design a graph with the animations (or blends paces or other state machines) in the AnimationTree panel. For example, you can have an Idle
animation connect to a Walking
animation and back.
In the transitions between the animations decide if they must wait for the animation to finish, in which case set their switch_mode
them to "At End" otherwise let them as "Immediate".
And you can write an expression that will control the transition between nodes in the advance_expression
property. For example, you can set that velocity.is_zero_approx()
for transitioning to the Idle animation. And with that Godot will be able to switch animations.
You might also want to set the xfade_time
and perhaps the xfade_curve
too also, as these control animation blending.
If you really want to manipulate the AnimationTree
from GDScript start by getting a reference to it in your script. You can drag it - with CTRL pressed - to your script to have Godot generate an @onready var
for it.
For example:
@onready var animation_tree:AnimationTree = $AnimationTree
With the AnimationTree
selected you can see which parameters it has in the inspector. Which you can then access with get
from GDScript (you can copy the property path from the context menu you get by secondary click on the property in the inspector).
For example:
var playback:AnimationNodeStateMachinePlayback = animation_tree.get("parameters/playback")
In the example we get the playback of the AnimationNodeStateMachine
, which we can use to tell the state machine to go to the node we want with travel
. For example:
playback.travel("some_state")
And, of course, if you need to manipulate the graph you can access the tree_root
of the AnimationTree
from code too.
Upvotes: 7