Elevator animation

Godot Version

4.2.1

Question

I want to create a Node2D with an elevator platform and AnimationPlayer inside, and export 2 variables:

  1. Length - indicating how many blocks the elevator will move.
  2. Direction - up (-1) or down (1).

Then, I want to place many elevators on my map using instantiated child scenes with different exported variables. When the scene starts, each elevator should generate a unique animation based on its exported variables.
For example: an elevator with length 3 and direction 1 (downwards) will generate an animation with position keys (0, 0) and (0, -16 * length * direction).

However, when I attempted to implement this, I encountered a problem where animations aren’t unique for each elevator, and animations are generated based on the settings of the last generated elevator.

Here’s my code (note: the direction is still implemented with just different animations, not as I described above):

extends Node2D

@export var is_downwards = true
@export_range(1, 100) var length: int = 1

@onready var animation = $AnimatableBody2D/Elevator_Animation.libraries[""]

@onready var fall_from_top = animation.get_animation("Fall_from_top")
@onready var fall_from_neutral = animation.get_animation("Fall_from_neutral")
@onready var lift_from_neutral = animation.get_animation("Lift_from_neutral")
@onready var lift_from_bottom = animation.get_animation("Lift_from_bottom")

func _ready():
    fall_from_neutral.track_set_key_value(0, 1, 16 * length)
    fall_from_top.track_set_key_value(0, 0, -16 * length)
    lift_from_bottom.track_set_key_value(0, 0, 16 * length)
    lift_from_neutral.track_set_key_value(0, 1, -16 * length)

Are there any methods to make animations unique or simpler ways to achieve my task?

Probably best to use tweens instead. This way you only generate end position when running the elevator.

2 Likes