Godot Version
Godot v4.2
Question
I am attempting to make a grid based movement system for a game I’m working on and I keep hitting roadblocks. The intention is to somewhat emulate the feel of old Pokemon games with their tile-based world. I have cobbled together some code but I really can’t seem to figure out how to get the character to move smoothly and without interruption.
extends CharacterBody2D
@export_category("Foundational")
@export var PlayerCamera: Camera2D
@export var sprDefault: AnimatedSprite2D
@onready var ray: RayCast2D = $RayCast2D
@onready var PlayerSprite: AnimatedSprite2D = sprDefault
var player_speed = 4
var moving = false
const tile_size = 16
const inputs = {
"player_right": Vector2.RIGHT,
"player_left": Vector2.LEFT,
"player_down": Vector2.DOWN,
"player_up": Vector2.UP,
}
func _unhandled_input(event):
if moving:
return
for dir in inputs.keys():
if event.get_action_strength(dir):
$PlayerSprite.play(dir)
move(dir)
func move(dir):
ray.target_position = inputs[dir] * tile_size
ray.force_raycast_update()
if !ray.is_colliding():
var tween = create_tween()
tween.tween_property(self, "position",
position + inputs[dir] * tile_size, 0.8/player_speed).set_trans(Tween.TRANS_LINEAR)
moving = true
await tween.finished
moving = false
This is my player code as is. It successfully follows the grid although its not exactly what I’m looking for. The player stutters as they walk (which stutters the camera as well) and the player will freeze for a moment before moving every time a movement key is pressed causing it to feel clunky and just awful.
What approach should I be coming at this from? Is there a way to fix this block or do I have to rewrite the whole thing? I just need a grid based movement solution that feels smooth while still being locked to the grid. Any help appreciated!