Home Game Development Player overshoots path utilizing NavigationAgent2D and NavigationPolygonInstance in Godot 3.5

Player overshoots path utilizing NavigationAgent2D and NavigationPolygonInstance in Godot 3.5

0
Player overshoots path utilizing NavigationAgent2D and NavigationPolygonInstance in Godot 3.5

[ad_1]

I’m making an attempt so as to add a click on to maneuver function in my recreation utilizing the brand new NavigationAgent2D and NavigationPolygonInstance however generally when the participant reaches the top of the navigation path or modifications route it overshoots the subsequent level so it will get caught there transferring forwards and backwards.

enter image description here

The logic to maneuver the participant is fairly straight ahead:

Player.tscn

extends KinematicBody2D

export var MAX_SPEED := 250

onready var navigation_agent := $NavigationAgent2D

var velocity := Vector2()

func _ready():
    set_target_location(place)
    
func set_target_location(goal: Vector2) -> void:
    navigation_agent.set_target_location(goal)

func _physics_process(_delta):
    if navigation_agent.is_navigation_finished():
        return
    
    var move_direction = place.direction_to(navigation_agent.get_next_location())
    velocity = move_direction * MAX_SPEED
    navigation_agent.set_velocity(velocity)

func _on_NavigationAgent2D_velocity_computed(safe_velocity):
    velocity = move_and_slide(safe_velocity)

World.tscn

extends Node2D

onready var participant := $Player

func _input(occasion):
   if occasion is InputEventMouseButton and occasion.pressed:
       var target_location = get_global_mouse_position()
       participant.set_target_location(target_location)

I attempted checking if the participant arrived by measuring the gap between its place and the navigation agent subsequent location and forcing it it cease if it will get shut. This works if the navigation is a straight line but when it is not then the participant stops as quickly because it wants to vary route:

func _physics_process(_delta):
    if navigation_agent.is_navigation_finished() || place.distance_to(navigation_agent.get_next_location()) < 4:
        return

    ...

I created a repo with some minimal code to breed the problem, hopefully it could actually assist:
https://github.com/jahvi/godot-sample

I believe ideally I have to test if my participant is inside a sure distance from the subsequent level and if that’s the case drive it to vary route however I’m undecided how to do that.

Can anybody advise the best way to keep away from this overshooting?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here