I have my pathfinding AI that uses a function to find a random point on the navmesh and pathfind there. the issue is that it randomlhy stopped working and im not sure what changed. the problem is that when it reaches the target, it is supposed to stop moving and pick a new pathfinding point, but instead it freezes in place but continues playing the walking animation and doesn’t continue pathfinding. i used a print(velocity) function to debug and it says that there is constant force of -2.5 on the Y axis but there is no gravity. here is where it gets very weird: 2.5 is the speed that it pathfinds at when it is not chasing the player, but for some reason it get applied to the Y axis. i have looked at the collision shapes and it is not getting stuck on anything. I have no idea what to do.
My advice would be first to stick a bunch of printing in, or better yet, put a UI label somewhere on the screen that you can scribble on. You need some context as to what’s happening and when.
It could be that something is wrong with your navigation logic. It could be that something is wrong with your timer logic. There are a variety of things that could be going wrong here.
The trick is to print stuff out so you know what is happening and what the code thinks it’s doing.
For example, I’d probably do something like:
if nav_agent.is_target_reached():
print("Target reached!")
velocity = Vector3.ZERO
if $"timers/look around".is_stopped():
print("Starting look around timer.")
$"timers/look around".start()
else:
print("Look around timer already running.")
if $timers/waitafterpathfind.is_stopped():
print("Starting wait after pathfind timer.")
$timers/waitafterpathfind.start()
else:
print("Wait after pathfind timer already running.")
And so forth. It’ll be spammy, but I bet you find things aren’t quite in the state you thought they were…
You might also want to look at mobile; it seems to lock out a lot of stuff when it’s false, including some of your state maintenance logic.
I think you should be able to call get_faces() instead of get_vertices(), and you’ll get back an array where each entry is a vertex, and they’re in groups of three (so the array length should be a multiple of three…), with each group of three being a triangle. Average those positions and you should be near the middle of your nav mesh triangle.
Ah, there’s get_polygon(index) and get_polygon_count(). So, use _count() to figure out which one to randomly grab. get_polygon() gets you an array of indexes for vertices, so you’ll need to march the list pulling vertices from get_vertices() and then average those.
it is better, but it still does not solve the issue with the AI
also does this look correct to you?
func pick_random():
if navregionpolygons > 0:
var navregion = $"../NavigationRegion3D"
var navmesh = navregion.navigation_mesh
var randomindex = randi() % navregionpolygons
var polygon_vertices = navmesh.get_polygon(randomindex)
var vertices = navmesh.get_vertices()
var vertice1 = vertices[polygon_vertices[0]]
var vertice2 = vertices[polygon_vertices[1]]
var vertice3 = vertices[polygon_vertices[2]]
var vertice_sum = vertice1+vertice2+vertice3
var vertice_average = vertice_sum / 3
var local_point = vertice_average
pathfind(local_point)
print("Going to navmesh point:", local_point)
$"../pathfindmarker".position = local_point
else:
print("navregionpositions is empty!")