Move forward is jittering

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

Hi

I have an object that use move_toward to follow the player. The movement works, but the object is jittering when the player moves as well (it moves fine when the player isn’t moving, but the object still needs to move to it’s wanted position).

the code:

func update_position(delta):
    var p: Vector2 = entered_body.global_position
    var c: Vector2 = self.global_position
    var target: Vector2 = p + min_distance * p.direction_to(c)

    if(entered_body.velocity.x < 0):
       	last_position = 35
    elif(entered_body.velocity.x > 0):
    	last_position = -35
    target.x += last_position
    self.global_position = c.move_toward(target, (150 + abs(target.x - global_position.x) + abs(target.y - global_position.y)) * delta)

Some explanations:

  • entered_bodyis the body (Usually the player) that the object needs to follow
  • min_distance is calculated by the amount of objects that need to follow the player. It starts as 0, and grow with the amount of objects that need to follow

Player movement code if needed:

velocity = Vector2(Input.get_action_strength("ui_right") - 
Input.get_action_strength("ui_left"), velocity.y)

velocity.x = velocity.x * move_speed * speed_multiplayer
velocity.y += gravity * delta

animationManager(delta)
	
if(velocity.x != last_velocity.x and velocity.x != 0):
	last_velocity.x = velocity.x
	
move_and_slide()
self.position.x = snapped(self.position.x, 1) 
self.position.y = snapped(self.position.y, 1) 

example video:

(Works better when downloading the video)

Sprites info:
Player - 32X32
Floppy-disk - 18X18

:bust_in_silhouette: Reply From: crossbito

Hi! From what I understand, the issue you’re experiencing may be due to the discrepancy between the disk’s movement towards the player, which depends on its position, and the player’s movement, which depends on the speed. As a result, the player moves smoothly, but the disk moves towards the player at a faster rate, causing the disk to stop and move forward in fast intervals when the player move. For example I think it’s like if the player where holding a rope with a rock attached to it. When the player moves forward, they reach the limit of the rope, causing them to push the rock forward. The rock then stops until the player reaches the limit of the rope again. This creates a pattern where the rock moves forward and stops periodically as the player moves.

Hi

Apparently, the documentation added a Godot 4 solution to the problem:
Interpolation — Godot Engine (stable) documentation in English

I wouldn’t find it if you weren’t pointing to the disk’s movement being the problem.

GameSchool | 2023-06-08 18:28