Movement script is jittery

Godot Version

4.5.1

Question

Have tried to write a movement script for an enemy in may top down game, howver, the movement is jittery and I cannot figure out why. Script:

func seeing_me():
print(“SEEN”)
while true:
rotation+=get_angle_to($“../My_Pirate”.position)-90
position.x=move_toward(position.x,$“../My_Pirate”.position.x, speed )
position.y=move_toward(position.y,$“../My_Pirate”.position.y, speed )
await get_tree().create_timer(1.0/60.0).timeout
var distance = position-$“../My_Pirate”.position
var diagonal_distance=sqrt(distance.x**2+distance.y**2)
if diagonal_distance>=seeing_distance:
seeing=false
print(“UNSEEN”)
break

Who and when calls seeing_me()?

Btw. paste your code block between ``` tags to format it properly:
```
code
```

func _process(delta: float) -> void:
	move_and_slide()
	if health<=0:
		queue_free()
	
	if seeing==false:
		var distance = position-$"../My_Pirate".position
		var diagonal_distance=sqrt(distance.x**2+distance.y**2)
		if diagonal_distance<=seeing_distance:
			seeing=true
			seeing_me()
func seeing_me():
	print("SEEN")
	while true:
		rotation+=get_angle_to($"../My_Pirate".position)-90
		position.x=move_toward(position.x,$"../My_Pirate".position.x, speed )
		position.y=move_toward(position.y,$"../My_Pirate".position.y, speed )
		await  get_tree().create_timer(1.0/60.0).timeout
		var distance = position-$"../My_Pirate".position
		var diagonal_distance=sqrt(distance.x**2+distance.y**2)
		if diagonal_distance>=seeing_distance:
			seeing=false
			print("UNSEEN")
			break

Never await in _process(). There are other apparent problems too but you should first implement a version that does not use await.

I think the function is only being called once so having await shouldn’t matter?

What do you mean by “called only once”? It’s called every frame if the condition is met. Besides, it runs an endless loop. In any case, you shouldn’t be using await nor endless loops in _process()

Never mind, I had a other object pushing againt it, it wasn’t because of that script

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.