Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Quote19 |
I am new to Godot. So, to learn I coded 2D platformer movement for my player by following this tutorial and I found a problem that my character is not playing Jump and Fall animation when I press jump but other animations are working fine for me.
extends KinematicBody2D
const UP_DIRECTION := Vector2.UP
export var speed := 50
export var gravity := 5000
export var jump_strength := 800.0
var _velocity := Vector2.ZERO
onready var pivot: Node2D = $AnimatedSprite
onready var _start_scale: Vector2 = pivot.scale
export var maximum_jumps := 1
var jumps_made := 0
func _physics_process(delta: float) -> void:
var horizontal_movement = (
Input.get_action_strength("move_forward")
- Input.get_action_strength("move_backward")
)
_velocity.x = horizontal_movement * speed
_velocity.y += gravity * delta
var is_jumping := Input.is_action_just_pressed("jump") and is_on_floor()
var is_moving := not is_zero_approx(_velocity.x)
var is_idling := is_zero_approx(_velocity.x)
var is_falling := _velocity.y > 0.0 and not is_on_floor()
if is_jumping:
jumps_made = 1
_velocity.y = -jump_strength
elif is_idling:
jumps_made = 0
elif is_moving:
jumps_made = 0
_velocity = move_and_slide(_velocity, UP_DIRECTION)
if not is_zero_approx(_velocity.x):
pivot.scale.x = sign(_velocity.x) * _start_scale.x
if is_jumping:
$AnimatedSprite.play("Jump")
if is_falling:
$AnimatedSprite.play("Fall")
if is_idling:
$AnimatedSprite.play("Idle")
if is_moving:
$AnimatedSprite.play("Walk")
Hi,
Does the character jump though, even though the animation isn’t played?
Is the animation in the sprite called Jump?
I can follow the code, but can’t test it without creating a character etc etc, so if you could upload the project I’d be happy to take a look and try and help.
deaton64 | 2021-12-26 19:38
I would say the most likely answer is that you have used different cases on the animation. If the animation doesnt have a capital j for example then there is nothing to call with the name Jump.
Gluon | 2021-12-26 19:56
Sorry for late response but i already fixed it. The problem was I was using if statement instead of elif.
if is_jumping:
$AnimatedSprite.play("Jump")
if is_falling:
$AnimatedSprite.play("Fall")
if is_idling:
$AnimatedSprite.play("Idle")
if is_moving:
$AnimatedSprite.play("Walk")
Quote19 | 2021-12-30 10:28
Now I changed that into this.
if is_jumping:
$AnimatedSprite.play("Jump")
elif is_falling:
$AnimatedSprite.play("Fall")
elif is_idling:
$AnimatedSprite.play("Idle")
elif is_moving:
$AnimatedSprite.play("Walk")
Quote19 | 2021-12-30 10:30