Smooth 2D Movement Over Tile Based

Godot Version

4.3.stable

Question

So my current movement code is like tile based similar to pokemon red and is coded like this

const tile_size = 16
var moving:bool = false 
var input_dir

func _physics_process(delta):
	input_dir = Vector2.ZERO
	if Input.is_action_pressed("MoveDown"):
		input_dir = Vector2(0,1)
		move()
	elif Input.is_action_pressed("MoveUp"):
		input_dir = Vector2(0,-1)
		move()
	elif Input.is_action_pressed("MoveLeft"):
		input_dir = Vector2(-1,0)
		move()
	elif Input.is_action_pressed("MoveRight"):
		input_dir = Vector2(1,0)
		move()


func move():
	if input_dir and moving == false:
		moving = true
		var tween = create_tween()
		tween.tween_property(self, "position", position + input_dir * tile_size, 0.35)
		tween.tween_callback(move_false)
		
	
func move_false():
	moving = false

What would be the best way yall could think of for making this just smooth movement like not grid based ??

Basic smooth movement can look something like this:

@export var speed = 100
@export var accel = 10

func _physics_process(delta):
	var direction: Vector2 = Input.get_vector("MoveLeft", "MoveRight", "MoveUp", "MoveDown")
	
	velocity.x = move_toward(velocity.x, speed * direction.x, accel)
	velocity.y = move_toward(velocity.y, speed * direction.y, accel)

	move_and_slide()

This is code for a characterbody2d

Thanks so much works perfect sorry to be a bother haha

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.