0 votes

My navigation agent script doesn't really work, and I don't know what the reason is. Everything I've written so far is right, at least based from the codebases that I've studied. It works by manipulating the transform directly, but I want to do it with the velocity from the kinematic body through moveandslide().

It's even more harder to debug since there's no other resources I can rely to regarding this problem. Note that I've already enabled Avoidance, and this kind of algorithm words in 2D, but somehow not in 3D.

extends KinematicBody

onready var navigationAgent = get_node("NavigationAgent")
onready var navigationServer = get_parent() # Parent must be a navigation node
onready var animationPlayer = get_node("AnimationPlayer")
onready var camera = navigationServer.get_node("Camera")

func _ready():
    navigationAgent.set_target_location(global_transform.origin)

func _unhandled_input(event):
    if Input.is_action_just_pressed("ui_left_mouse"):
        navigationAgent.set_target_location(camera.get_mouse_world_point()) # This bit of code just gets the mouse click position.

func _physics_process(delta):
    var nextLocation = navigationAgent.get_next_location()
    var direction = global_transform.origin.direction_to(nextLocation)
    var velocity = direction * navigationAgent.max_speed
    navigationAgent.set_velocity(velocity)

func _on_NavigationAgent_velocity_computed(safe_velocity):
    move_and_slide(safe_velocity, Vector3.UP)
Godot version Godot 3.5
in Engine by (19 points)

1 Answer

0 votes
Best answer

So apparently,

The height of the CollisionShape from the KinematicBody matters a lot, especially when using a capsule as the hitbox for your body. The Path Desired Distance variable of the Navigation Agent should be proportional with the height of the CollisionShape. For example, if the Path Desired Distance is 1, then the height shouldn't exceed 2.

There's nothing wrong with my code, it's just there's something wrong with my set up. So please keep in mind how you set up your environment.

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.