extends Node3D
# --- LA BARRITA PARA EL INSPECTOR ---
# Esto solo afectará a la animación de caminar
@export_range(0.1, 4.0, 0.05) var velocidad_pasos: float = 1.0
# ----------------------------------
var animacion_actual = "AGACHAPREPARA"
func _ready():
$AnimationPlayer.play(animacion_actual)
func _physics_process(_delta):
# --- CONTROL INTELIGENTE DE VELOCIDAD ---
# Si estamos caminando, usamos el valor de tu barrita.
# Si es cualquier otra animación, usamos la velocidad normal (1.0).
if animacion_actual == "CAMINA":
$AnimationPlayer.speed_scale = velocidad_pasos
else:
$AnimationPlayer.speed_scale = 1.0
# ----------------------------------------
# Salir del juego al presionar la tecla Escape
if Input.is_action_just_pressed("ui_cancel"):
get_tree().quit()
var alguna_tecla_pulsada = false
# Comprobamos si alguna de las acciones está siendo presionada
if Input.is_action_pressed("achazo") or \
Input.is_action_pressed("mouse_left") or \
Input.is_action_pressed("mouse_right") or \
Input.is_action_pressed("DESDEABAJOPUÑETASO") or \
Input.is_action_pressed("ANDAALANTECONW") or \
Input.is_action_pressed("ANDAATRASCONS") or \
Input.is_action_pressed("CORRECACA") or \
Input.is_action_pressed("ui_accept") or \
Input.is_action_pressed("PATADAMEDIA") or \
Input.is_action_pressed("A") or \
Input.is_action_pressed("D"):
alguna_tecla_pulsada = true
# Animación de ESPADAZO / PUÑETAZO
if Input.is_action_pressed("achazo") or Input.is_action_pressed("mouse_left"):
if animacion_actual != "PUÑETAZO":
$AnimationPlayer.play("PUÑETAZO")
animacion_actual = "PUÑETAZO"
# Animación de INSPECCIONA (Click derecho)
elif Input.is_action_pressed("mouse_right") or Input.is_action_pressed("DESDEABAJOPUÑETASO"):
if animacion_actual != "INSPECCIONA":
$AnimationPlayer.play("INSPECCIONA", 0.0)
animacion_actual = "INSPECCIONA"
# Animación de CAMINAR (W, A, D)
elif Input.is_action_pressed("ANDAALANTECONW") or Input.is_action_pressed("A") or Input.is_action_pressed("D"):
if animacion_actual != "CAMINA":
# El 1.5 de aquí es el tiempo de mezcla suave entre animaciones
$AnimationPlayer.play("CAMINA", 1.5)
animacion_actual = "CAMINA"
# Animación de SALTAR (S)
elif Input.is_action_pressed("ANDAATRASCONS"):
if animacion_actual != "SALTA":
$AnimationPlayer.play("SALTA")
animacion_actual = "SALTA"
# Animación de INSPECCIONA (E / CORRECACA)
elif Input.is_action_pressed("CORRECACA") or Input.is_action_pressed("E"):
if animacion_actual != "INSPECCIONA":
$AnimationPlayer.play("INSPECCIONA")
animacion_actual = "INSPECCIONA"
# Animación de PUÑETAZO (R / PATADAMEDIA)
elif Input.is_action_pressed("PATADAMEDIA") or Input.is_action_pressed("R"):
if animacion_actual != "PUÑETAZO":
$AnimationPlayer.play("PUÑETAZO")
animacion_actual = "PUÑETAZO"
# Si no tocamos nada, vuelve a la posición de espera (IDLE)
elif not alguna_tecla_pulsada:
if animacion_actual != "AGACHAPREPARA":
$AnimationPlayer.play("AGACHAPREPARA", 0.0)
animacion_actual = "AGACHAPREPARA"
-------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Explicacion: boy equilibrando los movimientos del personaje ahora la velocidad del movimiento de piernas y de caminar del personaje, es muy util variar el gdscript para que utilice @expor, ahora pasare el gdscript del characterbody, para variar por @export su velocidad de traslado por el escenario........
-----------------------------------------------------------------------------------------------------------------------
extends CharacterBody3D
var rotate_speed = 0.001
@export var SPEED: float = 3.0 # Velocidad normal de caminar
#const SPEED = 3.0 # Velocidad normal de caminar
const RUN_SPEED = 10.0 # Velocidad más alta para correr
const JUMP_VELOCITY = 2.5
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
# --- NUEVAS VARIABLES PARA EL GIRO SUAVE ---
var velocidad_giro_actual = 0.0
@export var velocidad_maxima_giro: float = 0.35
#var velocidad_maxima_giro = 0.35
var suavizado_giro = 0.01
# -------------------------------------------
func _ready():
pass
func _physics_process(delta):
# Gravedad, siempre activa
if not is_on_floor():
velocity.y -= gravity * delta
# El salto se activa al mantener pulsado
if Input.is_action_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
# Lógica de movimiento
var current_speed = SPEED
var direction = Vector3.ZERO
# Determinar dirección de avance/retroceso
if Input.is_action_pressed("CORRECACA"):
direction = -transform.basis.z
current_speed = RUN_SPEED
else:
# Nota: Usamos solo el eje vertical (Y) del vector para caminar adelante/atrás
# El eje horizontal (X) lo usaremos para rotar más abajo
var input_dir = Input.get_vector("GIRAIZQUIERDACONA", "GIRADERECHACOND", "ANDAATRASCONS", "ANDAALANTECONW")
direction = (transform.basis * Vector3(0, 0, input_dir.y)).normalized()
current_speed = SPEED
if direction:
velocity.x = direction.x * current_speed
velocity.z = direction.z * current_speed
else:
velocity.x = move_toward(velocity.x, 0, current_speed)
velocity.z = move_toward(velocity.z, 0, current_speed)
move_and_slide()
# --- LÓGICA DE GIRO SUAVE ---
var direccion_giro = 0.0
if Input.is_action_pressed("GIRAIZQUIERDACONA"):
direccion_giro += 0.5
if Input.is_action_pressed("GIRADERECHACOND"):
direccion_giro -= 0.5
# Calculamos el objetivo y suavizamos la velocidad
var objetivo = direccion_giro * velocidad_maxima_giro
velocidad_giro_actual = lerp(velocidad_giro_actual, objetivo, suavizado_giro)
# Aplicamos la rotación suave
rotate_y(velocidad_giro_actual)