Why is enemy jittering when player slows down ?

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

Guys I have an enemy of type KinematicBody2D which is supposed to follow player and stop at a certain distance when the player stops moving. Everything works and enemy follows player but when my player moves really slowly , the enemy starts to jitter very badly. I don’t understand why ? Here’s the code attached to the enemy :

var speed = 10000

onready var playerReference = get_parent().get_node("Player")

func _physics_process(delta: float) -> void:
	var direction = playerReference.get_position() - position
	var directionDistance = direction.length()
	var normalizedDirection = direction.normalized()
	if directionDistance > 80 and directionDistance <150:

The Enemy has AnimatedSprite, CollisionShape2D, Area2D as children. Also the player and the rest of the scene moves very smoothly, only enemy starts jittering when the player speed is slow. At high speed, the enemy also moves smoothly.

Wild guess, not tested:

use _process instead of _physics_process.

_process is called every frame, _physics_process only if something is “happening”.



This is wrong. I just read that move_and_slide should ALWAYS be in _physics_process.
Sorry for wrong information!

whiteshampoo | 2021-01-30 13:13

Yes I tried doing this but nothing happened. I even tried turning smoothing off on the Camera attached to Player but still nothing happened.

Scavex | 2021-01-30 13:15

gimme a second, i’ll try something with your code…

whiteshampoo | 2021-01-30 13:18

Cannot reproduce you problem. Can you share your player-script?

whiteshampoo | 2021-01-30 13:25

The player has a similar function but instead he follows mouse !

func moveTowardsMouse(): #To move towards the cursor
	if position.distance_to(get_global_mouse_position()) > stopDistance:
		var direction = get_global_mouse_position() - position
		var directionDistance = direction.length()
		var normalizedDirection = direction.normalized()
		move_and_slide(normalizedDirection*max(minSpeed, min(maxSpeed, directionDistance)),Vector2( 0, 0 ),false, 4, PI/4, false)

Scavex | 2021-01-30 13:28

Works fine at my PC.

btw, dont use delta in move_and_slide.
That may be the problem.

whiteshampoo | 2021-01-30 13:39

That is so strange ! I can’t seem to fix it even after removing delta ! These are the moments when I really feel disappointed.

Scavex | 2021-01-30 13:43

This happens to all of us sometimes :slight_smile:

Can you share your project-folder?
It’s hard with only snippets. sometimes the error is somewhere else.

whiteshampoo | 2021-01-30 13:46

Carnitine – Google Drive
There are not many scenes and scripts. It’s a rough prototype so you should be able to take a good look at everything in a short time. I really hope you can tell me what’s wrong ! I really want to understand what’s causing this jittering. Also if you go ahead and increase the speed of enemy then the jittering will be more noticeable.

Scavex | 2021-01-30 13:59

have you tried to remove delta and giving speed a lower value? (40 works good)
If this solves your problem, i an explain you why. If not, then something is strange, because i have no jitter at all.

whiteshampoo | 2021-01-30 16:53

Yes I removed delta and lowered the speed. Still I get jitter at high speeds. But overall I really appreciate your help :slight_smile:

Scavex | 2021-01-31 08:38

The problem with high speed is, that the enemy comes close very fast, then waits, because it is too close, then jumps at the player again. hope you understand what i mean…

whiteshampoo | 2021-01-31 09:30