Godot v4.5.1
Hi, I´m new to Godot and GDscript, any help is welcome. I want to save the last rotation of the 2DAgent but the method that i use to get it automatically “erases” after it finises the line of code.
extends Area2D
@onready var nav_agent: = $Navigation_agent
@onready var sprite_holder: Node2D = $Sprite_holder
@onready var sprite_2d: Sprite2D = $Sprite_holder/Sprite
@onready var debug_label: Label = $Debug_label
#Velocidad_agente
const speed := 50.0
#Velocidad_rotación_agente
var rotation_speed := 2.0
#Rotación_sprite_agente
var agent_rotation : Vector2
var safe_last_rotation: Vector2
func change_agent_rotation():
sprite_holder.rotation = agent_rotation.angle()
print (agent_rotation)
func _physics_process(_delta: float) -> void:
debug_label.text = "Done: %s\nHit_target: %s\nTarget_reachable: %s" % [
nav_agent.is_navigation_finished(),
nav_agent.is_target_reached(),
nav_agent.is_target_reachable()]
change_agent_rotation()
manual_navigation()
navigate_safe()
#navigate(delta)
func manual_navigation() -> void:
if Input.is_action_just_pressed("set_target"):
nav_agent.target_position = get_global_mouse_position()
#Más simple, no evita obstáculos
#func navigate(delta: float) -> void:
#if nav_agent.is_navigation_finished():
#return
#var next_path_position: Vector2 = nav_agent.get_next_path_position()
#var new_velocity: Vector2 = (global_position.direction_to(next_path_position) * speed)
#position += new_velocity * delta
#sprite_holder.rotation = new_velocity.angle()
func navigate_safe() -> void:
if nav_agent.is_navigation_finished():
return
var next_path_position: Vector2 = nav_agent.get_next_path_position()
var new_velocity: Vector2 = (global_position.direction_to(next_path_position) * speed)
nav_agent.velocity = new_velocity
func _on_navigation_agent_velocity_computed(safe_velocity: Vector2) -> void:
position+= safe_velocity * get_physics_process_delta_time()
agent_rotation = safe_velocity