'move_toward' seems to have a 1 pixel offset when moving my character

Godot Version

v4.1.2.stable.official [399c9dc39]

Question

I’ve been trying to code a grid movement system. It works for the most part, I’ve been able to correctly implement animated sprites that move and stop. However, I’ve noticed a sort of jitter in the sprite while I’m having it move from one tile to another. At first I thought it was the camera, but getting a close look at the sprite leads me to believe that the sprite moves one pixel to the left while moving, and then realigns itself one pixel to the right when the moving is finished, causing that jittery feeling. (I think the jittering occurs when moving up or down as well)

I double-checked the spritesheet and I am confident that the sprites are not accidentally in the wrong place, so I am inclined to believe the jittering is coming from the code, which is here:


@onready var ray = $raycast
@onready var sprite = $sprite

const tileSize = 16
const walkSpeed = 0.8
const runSpeed = 2 * walkSpeed

var isMoving = false
var newPosition = Vector2(position.x, position.y)
var playerDirection = {
	"key_up": Vector2.UP,
	"key_down": Vector2.DOWN,
	"key_left": Vector2.LEFT,
	"key_right": Vector2.RIGHT,
}


func _ready():
	RenderingServer.set_default_clear_color("#393939")


func _process(delta):
	moveCharacter()


func moveCharacter():
	# Update the position of the raycast to check for collision.
	if position == newPosition:
		isMoving = false
		for n in playerDirection.keys():
			if Input.is_action_pressed(n) and isMoving == false:
				ray.target_position = playerDirection[n] * tileSize
				ray.force_raycast_update()
				sprite.animation = n
				
				if not ray.is_colliding():
					sprite.play()
					newPosition += playerDirection[n] * tileSize
					isMoving = true
				else:
					sprite.stop()
		
		if not Input.is_action_pressed("key_up") and not Input.is_action_pressed("key_down") and not Input.is_action_pressed("key_left") and not Input.is_action_pressed("key_right"):
			sprite.stop()
	# Make the player be able to run if the run button is held down.
	if not Input.is_action_pressed("key_b"):
		position = position.move_toward(newPosition, walkSpeed)
	else:
		position = position.move_toward(newPosition, runSpeed)

If it helps, here’s how I constructed the player scene:
Area2D
-AnimatedSprite2D
-RayCast2D
-Camera2D

I’m stuck. I have no clue what’s causing my sprite to offset by 1 pixel to the left(or up) while move_toward is running. If you happen to know what the issue is, any help would be appreciated! Thank you in advance :((((

HOLY MOLY I FIGURED IT OUT.

First of all, I am a bumbling idiot, for it turns out the camera WAS causing the pixel misalignment to occur. I was so sure it wasn’t the camera because I thought the pixel offset happened even after I disabled the camera, but after rechecking it, it turns out it was completely fine when I had the camera disabled.

Second… Uhh, apparently in the project settings, turning off ‘Snap 2D Transforms to Pixel’ fixed the issue??? I completely forgot what prompted me to turn it on in the first place, but, turning it off has disabled camera jittering for me. So I suppose as I have solved it on my own accord now, I guess there is no reason to keep this thread up anymore.

2 Likes