Hello, one problem(again)

i’ve tried to make my character jump, but there’s a strange behaviour


if character not doing anything while jump - animation works correctly, but if he goes left or right animation changes to walking
how to fix that? i suppose i need to write some condition, like “while jump animation plays, every other animations stop”
here’s code for jump and animation

func yet_he_jumps(delta):
	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			current_state = State.Jump
			velocity.y = -600
			
	if !is_on_floor() and current_state == State.Jump:
		var direction = Input.get_axis("left", "right")
		velocity.x += direction*100*delta
func player_anim():
	if current_state == State.Idle:
		animated_sprite_2d.play("idle")
	elif current_state == State.Run:
		animated_sprite_2d.play("run_right")
	elif current_state == State.Jump:
		animated_sprite_2d.play("jump")

Please show the full codes, it is not enough.

extends CharacterBody2D

var current_state
@onready var animated_sprite_2d = $AnimatedSprite2D

enum State {Idle, Run, Jump}
func _ready():
	current_state = State.Idle


func _physics_process(delta):
	falling(delta)
	he_stands(delta)
	then_he_runs(delta)
	yet_he_jumps(delta)
	move_and_slide()
	player_anim()
	
func falling(delta):
	if !is_on_floor():
		velocity.y = 1000*delta
		
		
func he_stands(delta):
	if is_on_floor():
		current_state = State.Idle
		
func then_he_runs(delta):
	var direction = Input.get_axis("left", "right")
	if direction:
		velocity.x = direction*150
	else:
		velocity.x = move_toward(velocity.x, 0, 150)
	if direction != 0:
		current_state = State.Run
		animated_sprite_2d.flip_h = false if direction > 0 else true
func yet_he_jumps(delta):
	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			current_state = State.Jump
			velocity.y = -600
			
	if !is_on_floor() and current_state == State.Jump:
		var direction = Input.get_axis("left", "right")
		velocity.x += direction*100*delta
func player_anim():
	if current_state == State.Idle:
		animated_sprite_2d.play("idle")
	elif current_state == State.Run:
		animated_sprite_2d.play("run_right")
	elif current_state == State.Jump:
		animated_sprite_2d.play("jump")
	

Try this codes:

func then_he_runs(delta):
	var direction = Input.get_axis("left", "right")
	if direction:
		velocity.x = direction*150
	else:
		velocity.x = move_toward(velocity.x, 0, 150)
	if direction != 0 and is_on_floor():
		current_state = State.Run
		animated_sprite_2d.flip_h = false if direction > 0 else true
1 Like

oh, that was pretty easy, i could solve it if i had more patience, anyway thanks sir

2 Likes

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