domingo, 2 de marzo de 2025

Enemigo persigue a player en godot 4.3; GDScript del enemigo y del player; y lo nodos y graficos correspondientes;


 extends CharacterBody3D


const SPEED = 5.0

const ACCELERATION = 1.0 # Ajusta este valor para la velocidad de respuesta deseada

@onready var navAgent = $NavigationAgent3D

@onready var target = $"../Player"


func _physics_process(delta: float) -> void:

# Añadir gravedad.

if not is_on_floor():

velocity += get_gravity() * delta


if target: # Verifica si el objetivo existe

_update_target_position() # Actualiza la posición del objetivo primero


var currentLocation = global_transform.origin

var nextLocation = navAgent.get_next_path_position()

var nextVelocity = (nextLocation - currentLocation).normalized() * SPEED


velocity = velocity.move_toward(nextVelocity, ACCELERATION * delta) # Usa delta para suavizar la aceleración

move_and_slide()

else:

print("El objetivo (jugador) no existe.")


func _update_target_position():

if target:

navAgent.target_position = target.global_transform.origin

else:

print("El objetivo (jugador) no existe. No se puede actualizar la posición del NavigationAgent.")

----------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
Explicacion: el GDScript de encima es para poner al enemigo, es para Godot 4.3 en juego 3d, fijarse en la ilustracion en los nombres de los nodos y los graficos elementos del juego...... el GDScript  inferior es para mover al player.
--------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5


func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()