Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Script de Movimentação em Grid sem diagonal e com colisão perfeita usando raycast
- extends CharacterBody2D
- var grid_size = 128
- var direction = Vector2.ZERO
- @onready var wall_detector: RayCast2D = $RayCast2D
- func _ready() -> void:
- global_position = Vector2(192,192)
- func _input(event):
- if event is InputEventKey and event.is_action_pressed("ui_right") or event.is_action_pressed("ui_left") or event.is_action_pressed("ui_up") or event.is_action_pressed("ui_down"): #antes era #.pressed:
- move_player()
- func move_player():
- direction = Input.get_vector("ui_left","ui_right","ui_up","ui_down")
- # Proibir movimentação diagonal
- if abs(direction.x) > 0 and abs(direction.y) > 0:
- direction = Vector2.ZERO
- if direction != Vector2.ZERO:
- # Calcular a nova posição
- var new_position = position + direction * grid_size
- # Atualizar o RayCast2D com base na nova posição
- if direction.x != 0:
- wall_detector.target_position = Vector2(direction.x * grid_size, 0)
- elif direction.y != 0:
- wall_detector.target_position = Vector2(0, direction.y * grid_size)
- wall_detector.force_raycast_update()
- if !wall_detector.is_colliding():
- # Aplicar a nova posição se não houver colisão
- position = new_position
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement