I figured it out!
It turns out it wasn't my code that was the problem, it was the navmesh itself!
In the first screenshot, I showed the navmesh in the editor, but the centre of that mesh was at the corner:
So what I did was go to Blender, selected the navMesh in the file and applied the transform (Space > Search "Apply Object Transform"), which makes the centre of the area the centre of the mesh.
After rearranging the code a little bit, it worked!
My code as of right now, I just need to fix some minor issues with the movement of the AI themselves, but this works, and you guys have my full permission to use this for your own games. Assuming anyone here actually develops 3D games and needs an easy pathfinding code:
var velocity = Vector3()
export(int) var topSpeed = 5
export(int) var acceleration = 1
onready var _topSpeed = topSpeed
onready var _acceleration = acceleration
onready var decceleration = acceleration / 2
onready var currentTarget = get_node(target)
var currentDirection = Vector3()
export(float) var turnSpeed = 1
var enemySpotted = false
var followPath = Vector3Array()
var currentPathNode = 0
const PATH_COOLER = 0.3
onready var pathTimer = PATH_COOLER
func FollowTargetPath(target, delta, isEnemy):
if currentPathNode < followPath.size() and pathTimer > 0:
dist = get_translation().distance_to(followPath[currentPathNode])
if dist < minDistance:
currentPathNode += 1
translate(FollowTarget(followPath[currentPathNode], delta, isEnemy))
pathTimer -= delta
#translate(FollowTarget(target, delta, isEnemy))
followPath = Vector3Array()
currentPathNode = 0
pathTimer = PATH_COOLER
followPath = navMesh.get_simple_path(get_translation(), target, true)
#This is where the following logic happens, it basically slowly turns to the target, and walks forward until it's too close to it, where it returns (0,0,0) to let the function
# calling it know that it's too close to the target, for it to change target if it's not going after the enemy.
func FollowTarget(target, delta, isEnemy):
dist = get_translation().distance_to(getTargetPosition())
if dist > minDistance:
turn_to(target, turnSpeed, delta)
if abs(velocity.z) < _topSpeed:
velocity.z -= _acceleration * delta
nextAnim = wapen + "Running"
nextAnim = wapen + "Walking"
changeAnim("Staundin 4", wapen + "Staundin")
return Vector3(0, 0, 0)
#LevelMain.gd (Where the Navmesh is):
export(NodePath) var navigationMesh
onready var navMesh = get_node(navigationMesh) setget , getNavMesh
func getPath(start, end):
var begin = navMesh.get_closest_point(start)
var p = navMesh.get_simple_path(begin, end, false)
If you can't be bothered, however, you can try Gokudomatic2's eco-FPS-walker, linked in the other answer to this question by (I assume) the author themself.