#Playing animations in the correct location.

6 messages · Page 1 of 1 (latest)

halcyon scarab
#

I can't get the jump animation to play in the right location. The landing animation plays when the player lands. I'll share a quick video and the code to see if anyone can help me. Many thanks

#

Player Code

#

extends CharacterBody2D
class_name Player

const RunState = preload("res://Scripts/Player/RunState.gd")
const JumpState = preload("res://Scripts/Player/JumpState.gd")
const AttackState = preload("res://Scripts/Player/AttackState.gd")

@onready var hit_box: CollisionShape2D = $"Animations/Attack/Area2D/Hit Box"
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var vfx: AnimationPlayer = $VFX
@onready var attack_cooldown: Timer = $"Attack Cooldown"
@onready var game_over: Panel = $"../Game Over"
@onready var attack: Sprite2D = $Animations/Attack
@onready var jump: Sprite2D = $Animations/Jump
@onready var crouch: Sprite2D = $Animations/Crouch
@onready var fall: Sprite2D = $Animations/Fall
@onready var run: Sprite2D = $Animations/Run
@onready var air_attack: Sprite2D = $Animations/AirAttack

@onready var crouch_dust_particles: Sprite2D = $VFX/CrouchDustParticles
@onready var jump_dust_particles: Sprite2D = $VFX/JumpDustParticles
@onready var run_dust_particles: Sprite2D = $VFX/RunDustParticles

@onready var Particle_loc : Vector2 = Vector2(0,0)
@onready var descending : bool = false
@onready var accending : bool = false
@onready var remaining_jumps : int = 0

const JUMP_VELOCITY = -450.0
const COYOTE_TIME = 0.4

var coyote_time_remaining = 0.0
var can_attack = true
var state: State = null

func _ready() -> void:
_set_state(RunState.new(self)) # Instantiate the RunState with self as the player reference.
attack.visible = false
jump.visible = false
fall.visible = false
crouch.visible = false
air_attack.visible = false

func _physics_process(delta: float) -> void:
if is_on_floor():
coyote_time_remaining = COYOTE_TIME
Particle_loc = self.global_position - Vector2 (0, 32)
else:
coyote_time_remaining -= delta

# Determine if player is ascending or descending
if not is_on_floor():
    velocity += get_gravity() * delta
    if velocity.y > 0:  # Positive y-velocity means the player is falling
        descending = true
        accending = false
    elif velocity.y < 0:  # Negative y-velocity means the player is rising
        descending = false
        accending = true
    else:
        descending = false
        accending = false
else:
    # Reset flags when on the ground
    descending = false
    accending = false

# Delegate to the current state
if state:
    state.physics_process(delta)

# Handle GameManager position logic
if GameManager.Wait == false:
    if position.x != 64:
        if position.x > 64:
            velocity.x = -15
        elif position.x < 64:
            velocity.x = 15

# Apply movement and slide
move_and_slide()

func _input(event: InputEvent) -> void:
if state:
state.handle_input(event)

func _set_state(new_state: State) -> void:
if state:
state.exit()
state = new_state
state.enter()

func _on_attack_cooldown_timeout() -> void:
can_attack = true

func _on_area_2d_body_entered(body: Node2D) -> void:
if body.is_in_group("Enemy"):
body.hitanimation()
GameManager.Score += 1
pass # Replace with function body.

func _on_animation_player_animation_finished(anim_name: StringName) -> void:
if state:
state.animation_finished(anim_name)

func endgame():
game_over.visible = true
game_over.gameover()

#

Jump State

#

extends State

const RunState = preload("res://Scripts/Player/RunState.gd")
const FallState = preload("res://Scripts/Player/FallState.gd")
const AirAttackState = preload("res://Scripts/Player/AirAttackState.gd")

func enter() -> void:
player.remaining_jumps += 1
player.jump_dust_particles.global_position = player.Particle_loc # Adjust position
# Allow jumping only if the player is on the floor or has coyote time left
if player.remaining_jumps > 1 or player.coyote_time_remaining > 0:
player.vfx.play("Jump Particles") # Play the effect

    player.velocity.y = player.JUMP_VELOCITY
    player.coyote_time_remaining = 0  # Reset coyote time to prevent multiple jumps

    # Hide attack animation if active
    if player.attack.visible:
        player.attack.visible = false
else:
    # If the player shouldn't be able to jump, transition to falling instead
    player._set_state(FallState.new(player))

func physics_process(delta: float) -> void:
if player.is_on_floor():
player._set_state(RunState.new(player))
elif player.descending:
player._set_state(FallState.new(player))
player.animation_player.play("Jump")

func handle_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("Attack") and player.can_attack and !player.is_on_floor():
player._set_state(AirAttackState.new(player))

halcyon scarab
#

If someone here could help me figure this out I'd really appreciate it. I'm still pretty new to this.