Godot Version
Godot 4
Question
Heyy, i am simply creating a 2D platformer and i am doing the animations for the player with animation tree but i can’t deal with the jump animation, like it dont work while i use the exact same process as the run or idle, and it works for these, but not the jump.
Here is my code and my animation tree,
extends CharacterBody2D
@export var SPEED = 300.0
@export var JUMP_VELOCITY = -400.0
@export var gravity = 980
@export var is_combat_mode = false
@onready var ap = $AnimationPlayer
@onready var sprite = $Sprite2D
@onready var sm = $AnimationTree.get("parameters/playback")
@onready var anim = $AnimatedSprite2D
func _physics_process(delta):
#Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
#Jump.
if is_on_floor():
jump()
#Get the input direction and handle the movement/deceleration.
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
#anim.play("Run")
sm.travel("Run")
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
#anim.play("Idle")
sm.travel("Idle")
if direction != 0:
switch_direction(direction)
#print(velocity.y)
combat_mode()
move_and_slide()
#update_animations(direction)
#func update_animations(direction):
# if is_on_floor():
# if direction == 0:
# ap.play("Idle")
# else:
# ap.play("Run")
# else:
# if velocity.y <0:
# ap.play("Idle") #la faut remplacer par "jump"
# elif velocity.y >0:
# ap.play("Idle") #la faut remplacer par "fall"
# if is_combat_mode:
# if direction == 0:
# ap.play("CombatIdle")
# else:
# ap.play("CombatWalk")
func switch_direction(direction):
sprite.flip_h = (direction == -1)
sprite.position.x = direction * 5
# anim.flip_h = (direction == -1)
# anim.position.x = direction * 5
func combat_mode():
if Input.is_action_pressed("combatPose"): # && is_combat_mode == false:
is_combat_mode = true
#anim.play("Combat_Idle")
sm.travel("CombatIdle")
#print("Combat Mode")
if is_combat_mode && velocity.x > 0 || velocity.x <0:
#anim.play("Combat_Walk")
sm.travel("CombatWalk")
SPEED = 200
else:
is_combat_mode = false
#print("NoCombatMode")
SPEED = 600
func jump():
if Input.is_action_just_pressed("jump"):
velocity.y = JUMP_VELOCITY
if velocity.y < 0:
print("Jump Up")
#anim.play("JumpUp")
sm.travel("JumpUp")
if velocity.y > 0:
print("Fall")
sm.travel("Fall")