AI glitching out like crazy when trying to follow a path

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cobra!

I’m trying to make my AI follow the player by using pathfinding, but the problem is that it spazzes out like crazy, and I have no idea what’s causing it to do so.

Here’s my code:

func _process(delta):
    if targetSpottit:
        if notTooClose(): # basically checks the distance to the target
            if navMesh:
                followTargetPath(target.translation, delta)
                #testFunc(target.translation, delta)
            else:
                translate(followTarget(target.get_translation(), delta))
                navMesh = $"../Level"
            nextAnim = rinninAnimation
            closeEneuch = false
        elif !closeEneuch:
            look_at(minusY(navMesh.get_closest_point(target.translation)), Vector3(0, 1, 0))
            closeEneuch = true
        else:
            turn_to(navMesh.get_closest_point(target.translation), delta)
            attack_logic(delta)
    elif $TargetRay.is_colliding():
        setTargetSpotted(true, $TargetRay.get_collider())
    else:
        nextAnim = staundinAnimation
 
func followTargetPath(target, delta):
    if followPath.size() > 0:
        if currentPathNode < followPath.size() - 1 and pathTimer > 0:
            translate(followTarget(followPath[currentPathNode], delta))
            if translation.distance_to(followPath[currentPathNode]) < 10 and pathTimer > 0:
                currentPathNode += 5
                print("Next")
            pathTimer -= delta
        else:
            translate(followTarget(target, delta))
            if translation.distance_to(navMesh.get_closest_point(target)) > minDistance * 2:
                followPath = Array()
                currentPathNode = 0
                pathTimer = PATH_COOLER
    else:
        var begin = $"../Level".get_closest_point()
        var p = $"../Level".get_simple_path(translation, target, false)
        return Array(p)
        followPath = $"..".getPath(translation, target)
 
func notTooClose():
    return translation.distance_to(navMesh.get_closest_point(target.translation)) > minDistance

Here’s a video demonstrating the problem: https://youtu.be/Ba6wOIoyl30
Here’s a download link to the project itself: http://www.mediafire.com/file/5gcai78w7p5d5j7/project.7z

What am I doing wrong?

:bust_in_silhouette: Reply From: Cobra!

Somebody rewrote the method for me, and it now works without problem!

Here’s the code:

func followTargetPath(target, delta):
#Update the target position to the player ever so often.
pathTimer -= delta
if pathTimer <= 0.0 or followPath.size() == 0:
	pathTimer = PATH_COOLER
	followPath = $"../..".getPath(translation, target)
	currentPathNode = 0
elif currentPathNode > followPath.size() -1:
	return

var direction = (followPath[currentPathNode] - translation).normalized()

if velocity <= tapSpeed:
	velocity += _acceleration
move_and_collide(direction * delta * velocity)
turn_to(direction, delta)

# The current pathpoint is reached. Go to the next one.
if followPath[currentPathNode].distance_to(translation) < 1.0:
	# The last pathpoint has not yet been reached.
	if currentPathNode < followPath.size() - 5:
		currentPathNode += 5
	elif currentPathNode != followPath.size():
		currentPathNode += 1