Completly new to programming and Godot, I made a game where a character is always following the player, so when he's walking he's slower than the player except if the player "holds his hand" then his speed changes to match the players . When the distance between the two grows too big, he stops following the player speed == 0 , until the player comes near and holds his hand again. Somehow was able to code it , but when I try to set the animations which are hand drawn I'm keep getting error and can't figure out why.
There’s six animations so far :
when speed == 0 , animation sad where he curls up, and then a stand up animation when the player holds their hand again,
when speed == 100 (slow) he has an idle and a walk animation
when speed == 150 , same speed as player, idle holding hand and walk while holding hand.
This is one of the iterations I made which of course does not work, please help :< :
```extends CharacterBody2D
@exportexportexportexport var target_path : Nod@exportexportPath
@export var set_distance : float = 100.0
@export var follow_speed : float = 100.0
var target : Node2D
func _ready ():
target = get_node(target_path)
func _physics_process(delta: float) → void:
var to_target = target.global_position - global_position
var direction = to_target.normalized()
print (set_distance,“aaaa”, to_target.length())
if to_target.length() > set_distance :
global_position += direction * follow_speed * delta
if to_target.length() > set_distance + 300:
follow_speed = 0
if to_target.length() < set_distance :
if Input.is_action_just_pressed(“HoldHand”):
follow_speed = 150
if Input.is_action_just_released(“HoldHand”):
follow_speed = 100
animation()
func animation() :
var to_target = target.global_position - global_position
var direction = to_target
if follow_speed == 0 :
$AnimatedSprite2D.play(“Sad”)
if Input.is_action_just_pressed(“HoldHand”):
$AnimatedSprite2D.play(“StandUp”)
if follow_speed == 150 :
$AnimatedSprite2D.play(“StandUp”)
if direction.x != 0 :
$AnimatedSprite2D.play(“HandHoldWalk”)```
if follow_speed == 100 :
$AnimatedSprite2D.play("Idle")
if direction.x != 0 :
$AnimatedSprite2D.play("Walk")
Nothing sticks out to me as being obviously wrong. There’s some things you ought to do differently, like using is_equal_approx,but it shouldn’t be causing any errors.
Show us the error you get. Also make sure there are no warnings about variable shadowing.
You can format code on the forum using three backticks ``` for blocks or just single ticks for fragments.
the errors are that the character following is stuck playing the first frame of the animation, frozen and when we get too far he plays the “sad” animation on loop even though i set it as play once
There’s two kinds of “overwriting” that might be going on which might not be intended.
You set the animation based on if speed == whatever and then potentially overwrite it if direction.x is not 0. But that’s not really a problem if you always set the speed and direction in a particular way.
According to AnimationPlayer — Godot Engine (stable) documentation in English the play method remembers the current animation when resuming playback but doesn’t mention if it avoids restarting on successive calls. If it doesn’t, it might be stuck on the first frame because it’s constantly restarting the animation. Edit: This should be fine and correctly loop your following animations.
Somewhat related, are you remembering that you’ve been sad anywhere? If not the problem could be that you are repeatedly doing: check player distance → speed is 0 → play sad animation
The code i sent is the complete code i have set up for that 2D sprite.
how can I avoid the repetition then ? do i have to change the condition that checks the speed ?
It should be fine to repeatedly set your walking animations. For these I recommend just changing your code to use if .. elif, though that’s because it’s good practice.
To be extra sure, you can try adding a second, nested if statement to check if the animation is not already playing before you call play.
For the sad animation, try adding a bool at the top of your script called ‘is_sad’ set to false. When player distance is too far and ‘is_sad’ is false, play the sad animation and set ‘is_sad’ to true.
As soon as the player holds their hand, or when you otherwise decide, change animations and be sure to set ‘is_sad’ back to false.
This is where the “state machine” pattern comes in handy, but it’s ultimately an extension of this logic: you’ve got different modes (or states), and you might do stuff when you switch modes, and you might have different things happening depending on the mode.