Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0 NavigationServer not moving character #39692

Closed
voylin opened this issue Jun 19, 2020 · 6 comments · Fixed by #48028
Closed

4.0 NavigationServer not moving character #39692

voylin opened this issue Jun 19, 2020 · 6 comments · Fixed by #48028

Comments

@voylin
Copy link
Contributor

voylin commented Jun 19, 2020

Godot version:
Godot 4.0.dev.custom_build.9562fb900

OS/device including version:
Arch 5.7.4

Issue description:
Been following this article to know how the new navigation system is supposed to work: https://godotengine.org/article/navigation-server-godot-4-0
But the player doesn't move an inch. Checking the next location is also empty.

I started a completely new project and just followed the steps, again the same result, doesn't want to move at all.

Steps to reproduce:
This is the original script which I made:

extends RigidBody3D

onready var camera: Camera3D
const ray_length = 1000

export(float) var velocity

func _ready():
	# Getting the camera object
	camera = $"/root/Main_Node/Camera"
	$NavigationAgent3D.set_navigation($"/root/Main_Node/Scene/Navigation3D")
	$NavigationAgent3D.set_velocity(Vector3(0,2,0))

func _physics_process(delta):
	# Query the `NavigationAgent` to know the next free to reach location.
	var next_location = $NavigationAgent3D.get_next_location()
	var pos = get_global_transform().origin
	
	# Floor normal.
	var n = $RayCast3D.get_collision_normal()
	if n.length_squared() < 0.001:
		# Set normal to Y+ if on air.
		n = Vector3(0, 1, 0)
	
	# Calculate the velocity.
	var vel = (next_location - pos).slide(n).normalized() * velocity
	set_linear_velocity(vel)
	
	# Tell the agent the velocity.
	$NavigationAgent3D.set_velocity(vel)

func _on_NavigationAgent3D_velocity_computed(safe_velocity):
	# Move the character using the computed `safe_velocity` and avoid dynamic obstacles.
	set_linear_velocity(safe_velocity)

func _input(event) -> void:
	if Input.is_action_just_pressed("left_click"):
		if $NavigationAgent3D.is_target_reachable():
#			var from = camera.project_ray_origin(event.position)
#			var to = from + camera.project_ray_normal(event.position) * ray_length
			print($"/root/Main_Node/Scene/Navigation3D/NavigationRegion3D/MeshInstance3D".global_transform.origin)
			$NavigationAgent3D.set_target_location($"/root/Main_Node/Scene/Navigation3D/NavigationRegion3D/MeshInstance3D".global_transform.origin)
#			print($NavigationAgent3D.is_navigation_finished())

And then I decided to start an empty project, followed everything step by step and used this code:

extends RigidBody3D

export(float) var velocity
export(NodePath) var target_node_1

func _ready():
	$NavigationAgent3D.set_target_location(get_node(target_node_1).get_global_transform().origin)

func _physics_process(_delta):
	# Query the `NavigationAgent` to know the next free to reach location.
	var target = $NavigationAgent3D.get_next_location()
	var pos = get_global_transform().origin

	# Floor normal.
	var n = $RayCast3D.get_collision_normal()
	if n.length_squared() < 0.001:
		# Set normal to Y+ if on air.
		n = Vector3(0, 1, 0)

	# Calculate the velocity.
	var vel = (target - pos).slide(n).normalized() * velocity

	# Tell the agent the velocity.
	$NavigationAgent3D.set_velocity(vel)

func _on_NavigationAgent_velocity_computed(safe_velocity):
	# Move the character using the computed `safe_velocity` and avoid dynamic obstacles.
	set_linear_velocity(safe_velocity)

Is this a bug or am I missing something in the script? Followed everything and adjusted the script to the new names (adding 3D behind most of the stuff)

@Calinou

This comment has been minimized.

@Calinou Calinou changed the title [bug] 4.0 Navigation server not moving character 4.0 NavigationServer not moving character Jun 19, 2020
@voylin

This comment has been minimized.

@kazi7
Copy link

kazi7 commented Aug 31, 2020

Godot version:
v4.0.dev.custom_build.f10c3810b

OS/device including version:
Windows 10 64bit

Just to give more information: If you call NavigationAgent.set_target_location() from _ready(), the character moves once to the target location. NavigationAgent.is_navigation_finished() returns true the whole time the character moves.

I used the same script as described in the Navigation Server article: https://godotengine.org/article/navigation-server-godot-4-0

@Tzelon
Copy link

Tzelon commented Jan 14, 2021

I put a repo with an example project based on the article.
If someone manages to get it working I would love a PR.
https://github.com/Tzelon/navigation_demo

@smix8
Copy link
Contributor

smix8 commented Apr 20, 2021

@voylin
@kazi7
@Tzelon

The collision avoidance callbacks were bugged and never send.
This is fixed with #48028.

Here are examples how to move Node3D, KinematicBody3D or RigidBody3D.

extends Node3D
# script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance

@export var movement_speed : float = 4.0
@onready var navigation_agent : NavigationAgent3D = get_node("NavigationAgent3D")
var movement_delta : float

func set_movement_target(movement_target : Vector3):
    navigation_agent.set_target_location(movement_target)

func _physics_process(delta):

    movement_delta = move_speed * delta
    var next_path_position : Vector3 = navigation_agent.get_next_location()
    var current_agent_position : Vector3 = global_transform.origin
    var new_velocity : Vector3 = (next_path_position - current_agent_position).normalized() * movement_delta
    navigation_agent.set_velocity(new_velocity)

func _on_NavigationAgent3D_velocity_computed(safe_velocity : Vector3):
    # Move Node3D with the computed `safe_velocity` to avoid dynamic obstacles.
    global_transform.origin = global_transform.origin.move_toward(global_transform.origin + safe_velocity, movement_delta)
extends KinematicBody3D
# script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance

@export var movement_speed : float = 4.0
@onready var navigation_agent : NavigationAgent3D = get_node("NavigationAgent3D")

func set_movement_target(movement_target : Vector3):
    navigation_agent.set_target_location(movement_target)

func _physics_process(delta):
    
    var next_path_position : Vector3 = navigation_agent.get_next_location()
    var current_agent_position : Vector3 = global_transform.origin
    var new_velocity : Vector3 = (next_path_position - current_agent_position).normalized() * movement_speed
    navigation_agent.set_velocity(new_velocity)

func _on_NavigationAgent3D_velocity_computed(safe_velocity : Vector3):
    # Move KinematicBody3D with the computed `safe_velocity` to avoid dynamic obstacles.
    move_and_slide(safe_velocity, Vector3.UP)
extends RigidBody3D
# script on agent parent node, connect the agent 'velocity_computed' signal for collision avoidance

@onready var navigation_agent : NavigationAgent3D = get_node("NavigationAgent3D")

func set_movement_target(movement_target : Vector3):
    navigation_agent.set_target_location(movement_target)

func _physics_process(delta):
    
    var next_path_position : Vector3 = navigation_agent.get_next_location()
    var current_agent_position : Vector3 = global_transform.origin
    var new_velocity : Vector3 = (next_path_position - current_agent_position).normalized() * velocity
    navigation_agent.set_velocity(new_velocity)
    
func _on_NavigationAgent3D_velocity_computed(safe_velocity : Vector3):
    # Move RigidBody3D with the computed `safe_velocity` to avoid dynamic obstacles.
    set_linear_velocity(safe_velocity)

If the default navigation map RID does not work for your use case you can change it after the ready notification directly on the NavigationServer3D and create a new callback for the collision avoidance.

func _ready():
    
    var navigation_agent : NavigationAgent3D = get_node("NavigationAgent3D")
    var agent_rid : RID = navigation_agent.get_rid()
    var map_rid : RID = navigation_agent.get_parent().get_world_3d().get_navigation_map()    
    NavigationServer3D.agent_set_map(agent_rid, map_rid)
    NavigationServer3D.agent_set_callback(agent_rid, navigation_agent, "_avoidance_done")

@akien-mga
Issue can be closed with merged fix and examples. Examples and docs will be part of another pr.

@akien-mga akien-mga added this to the 4.0 milestone Apr 20, 2021
@akien-mga
Copy link
Member

@smix8 You can add "Fixes #39692" to your PR and this will be closed automatically if/when the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants