+2 votes

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.

Godot version v3.2.3
in Engine by (416 points)

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!

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

gimme a second, i'll try something with your code...

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

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)

Works fine at my PC.

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

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

This happens to all of us sometimes :)

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

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.

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.

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

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...

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.