0
\$\begingroup\$

I am making a project in Godot 4.3 I am using a gridmap for the map and I have the NavMesh set up correctly, when I am in game I can get it to show me its pathing and it will update correctly accordingly. But for some reason the move_and_slide() function isn't working at all and my NPC is not moving. Any help would be greatly appreciated.

Picture of Pathing

Here is the code in my NavigationAgent3D, I can provide more if needed.

extends NavigationAgent3D

@export var main : Node
@export var manager_ : Node
@export var hurtBoxCollisionShape : CollisionShape3D
var SPEED = 3.0

var reached_target: bool = true


func _ready():
    return
    target_desired_distance = hurtBoxCollisionShape.shape.radius

func _on_target_reached():
    reached_target = true
    manager_.entity_state = manager_.ENTITY_STATE.ATTACK
    manager_.can_move = false

func _physics_process(delta):
    if main and main.player:
        target_position = main.player.global_transform.origin
        #print ("Player Position: ", main.player.global_transform.origin) 

    if distance_to_target() > target_desired_distance:
        reached_target = false
        #print("Distance to target: ", distance_to_target())
        manager_.entity_state = manager_.ENTITY_STATE.CHASE

        if not reached_target:
            var current_location = main.global_transform.origin
            var next_location = get_next_path_position()
            var new_velocity = (next_location - current_location).normalized() * SPEED
            set_velocity(new_velocity)

    if not main.is_on_floor():
        main.velocity.y -= main.gravity * delta

func _on_velocity_computed(safe_velocity):
    main.velocity = main.velocity.move_toward(safe_velocity, .25)
    main.move_and_slide(main.velocity)`
\$\endgroup\$
2
  • \$\begingroup\$ Is your code after return in ready executed? \$\endgroup\$
    – Zibelas
    Commented Aug 25 at 7:58
  • \$\begingroup\$ Woops. That's not whats causing this issue sadly though, just forgot to get rid of that when I commented a section and uncommented it out. \$\endgroup\$ Commented Aug 25 at 16:10

1 Answer 1

0
\$\begingroup\$

I suspect you forgot to connect _on_velocity_computed to the velocity_computed signal. To be clear, there isn't a _on_velocity_computed virtual method, you need to connect it. We can do that from code:

func _ready() -> void:
    velocity_computed.connect(_on_velocity_computed)

And then... Assuming your main is a CharacterBody3D... You will have an error because move_and_slide does not take arguments:

func _on_velocity_computed(safe_velocity:Vector2) -> void:
    main.velocity = main.velocity.move_toward(safe_velocity, .25)
    main.move_and_slide()

To be clear, you do not pass the velocity to move_and_slide in Godot 4.x (you did that in Godot 3.x, make sure you are looking at updated tutorials/documentation). Instead you set the velocity before calling move_and_slide, which you are already doing.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .