Godot Version
4.3
Question
i made a character that the player can control and it moves via tiles and tilemap, i wanna make it so that the player can move forever when an arrow key is pressed (and moving to the direction of said arrow key) until it hits a wall (or non-walkable tile)
The code
extends Sprite2D
class_name PacMan
@export var move_speed: float = 1
@onready var tile_map: TileMap = $"../TileMap"
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer
var is_moving = false
func _physics_process(delta: float) -> void:
if is_moving == false:
return
if global_position == sprite_2d.global_position:
is_moving = false
return
sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)
func _ready() -> void:
animation_player.play("chomping")
func _process(delta: float) -> void:
if is_moving:
return
if Input.is_action_pressed("up"):
rotation_degrees = 90
move(Vector2.UP)
is_moving = true
elif Input.is_action_pressed("down"):
rotation_degrees = 270
move(Vector2.DOWN)
is_moving = true
elif Input.is_action_pressed("left"):
rotation_degrees = 0
move(Vector2.LEFT)
is_moving = true
elif Input.is_action_pressed("right"):
rotation_degrees = 180
move(Vector2.RIGHT)
is_moving = true
func move(direction: Vector2):
var current_tile: Vector2i = tile_map.local_to_map(global_position)
var target_tile: Vector2i = Vector2i(
current_tile.x + direction.x,
current_tile.y + direction.y,
)
var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
if tile_data.get_custom_data("walk_tiles") == false:
return
#Player movement
is_moving = true
global_position = tile_map.map_to_local(target_tile)
sprite_2d.global_position = tile_map.map_to_local(current_tile)