How to make a character "turn_around" with animation tools?

Godot Version

4.0

Question

I’m trying to make my player character turn around after running to either left or right
I have a running animation, turn around animation, and an animation tree set up
But I’m having trouble figuring out how to make the character turn around animation play at the right time. I’ve tried a couple of different blendspace1d setups but cant get it working

I was wondering if a blendtree could work but I’m not that familiar with it or if there would be another solution

Any and all help is appreciated

Now, I’m a complete Godot beginner and have no idea what a beldtree is.
But here is my approach to it.

# var oldDirection saves direction from last frame
var direction :=  Input.get_axis("left", "right")
   if direction:
      velocity.x = direction * SPEED 
      #sprite flip
   if direction > 0 && oldDirection < 0 :
      animated_sprite_2d.play("turn right")
   if direction < 0 && oldDirection > 0 : 
      animated_sprite_2d.play("turn left")
else:
   velocity.x = move_toward(velocity.x, 0, SPEED)

Then your animations for turn left / right just must be quick for it to look good.

I don’t know if this was even remotly helpfull but “Any and all help is appreciated” is everything i saw xD

I can see what you are trying to do with the code and i think that with some tweaking it could work very well but i’m already using an animationtree and i don’t want to switch the way the player is animated and i don’t really know how i would implement this into what i currently

ps. I’m a beginner too so no worries :))

Hi, what do you mean by “turn around after running to either left or right”? Do you mean that they will stop running and play a turn around animation? When is the turn around animation triggered? Is the turn around animation and the running animation in the same BlendSpace1D?

1 Like

" what do you mean by “turn around after running to either left or right”? Do you mean that they will stop running and play a turn around animation?"

Yes, but the turnaround animation will be short

“When is the turn around animation triggered?”

When the player changes direction

Is the turn around animation and the running animation in the same BlendSpace1D?

I would like that yes, since a lot of work is dependent on that blendspace1d but I can fix the rest if a good solution is found

Have you tried changing the blend position value to a value that is closer to the turning around animation than the running animation?

1 Like

I’m not sure i understand completely but i think yes and the reason this dosent work is because the blendspace1d blend_value is dependent on the players direction.x value so when going from one direction to the other it triggers all animations in the blendspace1d the problem here is that I also have my idle animation in the middle (blend value 0.0)

I’ve been thinking about discarding the blendspace1d idea and trying to see if something else could work

I don’t know if you have any experience with blendspace1d but an idea I had was to implement two extra blendspace1d inside the blendspace1d so the node would look sort of like this:

-1.0_____________0.0_______1.0
[Blendspace1d] [Idle] [Blendspace1d]

Where inside the blendspace1d nodes it would look like this

-1.0______0.0_______1.0
[Run] __________ [Turn_around]

And vice versa.

And as mentioned before my first blendspace1d blend value is dependant on player direction.x but will my two other blendspace blend values also depend on player direction?

And if not how would i call their blend_value parameters?

I think it would be easier to use a state machine instead of nested blendspaces.

1 Like

Here is the solution

class_name Groundstate
extends Player_State
#-------------------------------------------
@export var JUMP_VELOCITY = -450.0
@export var air_state : Player_State
@export var death_state : Player_State
@export var hurt_state : Player_State
@export var fall_state : Player_State
@export var attack_state : Player_State
@export var crouch_state : Player_State
@export var dash_state : Player_State
@export var Turn_state : Player_State
@export var Turn_node : String = "Turn_around"
@export var Attack1_node : String = "Attack1"
@export var Crouch_down_node : String = "Crouch_down"
@export var Dash_node : String = "Dash"

var latest_direction = 1 # Now stored as a class member

#-------------------------------------------
func state_input(event : InputEvent):
	if event.is_action_pressed("Jump") and character.is_on_floor():
		Jump()
	if event.is_action_pressed("Melee1"):
		Attack()
	if event.is_action_pressed("ui_down") and character.is_on_floor():
		Crouch()
	if event.is_action_pressed("Dash") and character.is_on_floor():
		Dash()

#-------------------------------------------
func Crouch():
	playback.travel(Crouch_down_node)
	next_state = crouch_state

#-------------------------------------------
func Attack():
	playback.travel(Attack1_node)
	next_state = attack_state

#-------------------------------------------
func Jump():
	character.velocity.y = JUMP_VELOCITY
	next_state = air_state

#-------------------------------------------
func Dash():
	playback.travel(Dash_node)
	next_state = dash_state

#-------------------------------------------
func state_process(_delta):
	var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	character.SPEED = 300.0

	if direction.x != 0:
		if latest_direction != direction.x:
			# If facing direction changes, transition to turn state
			playback.travel(Turn_node)
			next_state = Turn_state

		latest_direction = direction.x  # Update latest direction

	# Check for hurt and death conditions
	if Game.hurt and Game.playerHP > 0:
		next_state = hurt_state
	elif Game.playerHP <= 0:
		next_state = death_state


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.