4
I am trying to make an animation play upon landing and it is not working and i am unsure of why.
if velocity.x == 0 and velocity.y < 0 and is_on_floor():
animated_sprite.play("Fall")
4
I am trying to make an animation play upon landing and it is not working and i am unsure of why.
if velocity.x == 0 and velocity.y < 0 and is_on_floor():
animated_sprite.play("Fall")
It’s hard to say without any other information but I think you should change “velocity.y < 0” to “velocity.y >= 0”. Velocity.y < 0 (negative velocity) likely means that your character is going up.
I apologize for the lack of info, here is the rest of the code
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var animated_sprite = $Sprite2D/AnimationPlayer
@onready var jump_anim: AnimationPlayer = $Sprite2D/AnimationPlayer
var landing : bool
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("MoveLeft", "MoveRight")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.x > 0 and velocity.y == 0:
$Sprite2D.flip_h = false
animated_sprite.play("Run")
if velocity.x < 0 and velocity.y == 0:
animated_sprite.play("Run")
$Sprite2D.flip_h = true
if velocity.x == 0 and velocity.y >= 0 and is_on_floor() :
animated_sprite.play("Fall")
print("Land")
if velocity.x == 0 and velocity.y == 0 :
animated_sprite.play("Idle")
if Input.is_action_just_pressed("Jump") and is_on_floor():
animated_sprite.play("Jump")
velocity.y = JUMP_VELOCITY
print("space")
if velocity.y < 0 and is_on_floor():
animated_sprite.play("Jump")
# Replace with function body.
move_and_slide()
After the change of velocity, my idle animation is now playing which i believe is stopping the landing animation from occurring
because is is_on_floor()
better way is create RayCast2D and check when is not is_on_floor() and raycast is_colliding, start playing landing animation, and when is_on_floor() start play idle
Thank you
After the changes the animation is now playing despite not being anywhere close to the ground, i tried changing the raycast direction to up just to check however the problem persisted. I think it might be colliding with the parent object, but i am unsure of why this would occure due to collid with parent being disabled
if velocity.x == 0 and velocity.y > 0 and $RayCast2D.is_colliding and !is_on_floor() :
animated_sprite.play("Fall")
print("Land")
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.