![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | scooty |
Hello! I’m working on a top-down shooter, and I’m trying to make an enemy that uses pathfinding to make its way to the player
I followed an old tutorial on Youtube and modified the results to fit with my game, but the size of the path table remains a consistent zero regardless of how much distance there is between the player and the enemy
var speed = 150
var nav = null setget set_nav
var path = []
var goal = Vector2()
func _ready():
set_physics_process(true)
goal = get_tree().get_root().get_node("Node2D/Player").position
func set_nav(new_nav):
new_nav = get_tree().get_root().get_node("Node2D/Navigation2D")
nav = new_nav
update_path()
func update_path():
path = nav.get_simple_path(position, goal, false)
if path.size() == 0:
pass
func _physics_process(delta):
print(path.size())
if path.size() > 1:
print("okay?")
var d = position.distance_to(path[0])
if d > 2:
position = position.linear_interpolate(path[0], (speed * delta)/d)
else:
path.remove(0)
Some help with this would be greatly appreciated
Is goal
being set properly - is the Player node lower in the scene tree hierarchy than your enemy node? Use this to see if it is, check the debugger to see the value of goal
when the game stops:
func _ready():
set_physics_process(true)
goal = get_tree().get_root().get_node("Node2D/Player").position
breakpoint
timothybrentwood | 2021-05-04 02:14
Yep, the coordinates of goal are the same as the player. And the player and enemy are both on the same level of hierarchy
scooty | 2021-05-04 02:28
Three things:
- Is your Navigation2D node
_ready()
at the time you callset_nav()
on your enemy? Add print statements to both to make sure. - Are you using a NavigationPolygon or a TileMap with Navigation2D node?
- Try hard coding a value to
get_simple_path()
and see if you get a path:
:
func update_path():
path = nav.get_simple_path(Vector2(0,0), Vector2(15,15), false)
timothybrentwood | 2021-05-04 02:42