domingo, 4 de octubre de 2026

GDScript perfecto para animar personaje; para Godot 4.7 en 3d;

 extends Node3D


var anim_player: AnimationPlayer


# Nombres de las animaciones para tener el código limpio

const ANIM_RELAX: String = "MakeHuman default skeleton|RELAX"

const ANIM_ANDAR: String = "MakeHuman default skeleton|ANDAR"

const ANIM_ZARPAZO: String = "MakeHuman default skeleton|ZARPAZO"

const ANIM_MANOTAZO: String = "MakeHuman default skeleton|MANOTAZO"

const ANIM_FURIA: String = "MakeHuman default skeleton|FURIA"


func _ready() -> void:

# Busca el AnimationPlayer dentro de este nodo y sus hijos

anim_player = buscar_animation_player_propio(self)

if anim_player:

print("¡AnimationPlayer propio del personaje encontrado con éxito!")

# Configura RELAX y ANDAR para que se reproduzcan en bucle

asegurar_loop(ANIM_RELAX)

asegurar_loop(ANIM_ANDAR)

# Arranca en RELAX

reproducir(ANIM_RELAX)

else:

print("ERROR: No se encuentra ningún AnimationPlayer dentro de este personaje.")


func buscar_animation_player_propio(nodo: Node) -> AnimationPlayer:

if nodo is AnimationPlayer:

return nodo as AnimationPlayer

for hijo in nodo.get_children():

var resultado = buscar_animation_player_propio(hijo)

if resultado:

return resultado

return null


func _unhandled_input(event: InputEvent) -> void:

if not anim_player:

return


# -------------------------------------------------------------

# 1. CUANDO SE PRESIONA UNA TECLA O BOTÓN (ACCIONES)

# -------------------------------------------------------------

if event.is_pressed() and not event.is_echo():

# Clic Izquierdo -> ZARPAZO

if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:

reproducir(ANIM_ZARPAZO)

return

# Clic Derecho -> MANOTAZO

if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT:

reproducir(ANIM_MANOTAZO)

return


# Teclado

if event is InputEventKey:

match event.keycode:

KEY_W:

reproducir(ANIM_ANDAR)

KEY_P:

reproducir(ANIM_ZARPAZO)

KEY_I:

reproducir(ANIM_FURIA)

KEY_O:

reproducir(ANIM_RELAX)


# -------------------------------------------------------------

# 2. CUANDO SE SUELTA CUALQUIER TECLA O BOTÓN -> VOLVER A RELAX

# -------------------------------------------------------------

if not event.is_pressed():

# Si soltamos cualquier botón/tecla y no queda NINGUNA otra pulsada, pasa a RELAX

if not hay_entrada_activa():

reproducir(ANIM_RELAX)


# Comprueba si el usuario tiene mantenida alguna tecla o botón de acción

func hay_entrada_activa() -> bool:

return Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) \

or Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) \

or Input.is_key_pressed(KEY_W) \

or Input.is_key_pressed(KEY_P) \

or Input.is_key_pressed(KEY_I) \

or Input.is_key_pressed(KEY_O)


# Activa el modo Loop en Godot para las animaciones continuas

func asegurar_loop(nombre: String) -> void:

if anim_player.has_animation(nombre):

var anim = anim_player.get_animation(nombre)

anim.loop_mode = Animation.LOOP_LINEAR


func reproducir(nombre: String) -> void:

if anim_player.has_animation(nombre):

# Solo cambia si la animación solicitada no es la que ya está sonando

if anim_player.current_animation != nombre:

anim_player.play(nombre)

print("Reproduciendo: ", nombre)

else:

print("La animación '", nombre, "' no existe en este AnimationPlayer.")

No hay comentarios:

Publicar un comentario