How to add a 4 dimensional feel on KinematicBody2D movement?

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

I have a simple KinematicBody2D that follows a path from Navigation2D->NavigationPolygonInstance, with a few adjustment on movement to add my target movement feel.

All is well but for one thing, when the node creates a diagonal turn then full straight line the character starts to jitter up-down or left-right.

extends KinematicBody2D

var move_speed = 200
var path : = PoolVector2Array()

func _ready():
	position = position.snapped(Vector2.ONE * Globals.TILE_SIZE)


func _process(delta):
	# Calculate the movement distance for this frame
	var distance_per_frame = move_speed * delta
	move_along_path(distance_per_frame)
			

func move_along_path(distance_per_frame : float):
	# Move the player along the path until he has run out of movement or the path ends.		
	while distance_per_frame > 0 and path.size() > 0:
		var path_snapped = path[0].snapped(Vector2.ONE * Globals.TILE_SIZE)
		var distance_to_next_point = position.distance_to(path_snapped)
		if distance_per_frame <= distance_to_next_point and distance_per_frame >= 0.0:
			# The player does not have enough movement left to get to the next point.
			var direction = ( path_snapped - position ).normalized()
			# sign x,y to give a 4D direction movement feel
			var dir_x = sign(direction.x)
			var dir_y = sign(direction.y)
			var movement = Vector2(dir_x, dir_y) * distance_per_frame
			position += movement
			print_debug(dir_x, dir_y)
			break
		else:
			# The player get to the next point
			position = path_snapped
			path.remove(0)
			break

The reason was after making the diagonal turn the dir_x and dir_y will never return 0 that makes it jitter even thought it starts moving a straight path.

Been trying to figure this out for few days already with no luck.

:bust_in_silhouette: Reply From: johnguild26

Finally removed he jittering by updating the code to

var dir_x = sign(round(direction.x))
var dir_y = sign(round(direction.y))