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 :((((