Created
March 5, 2025 20:48
-
-
Save lamarmarshall/0ba15bf909faa56596feb999a99c15b4 to your computer and use it in GitHub Desktop.
godot, topdown 2d, tile, character controller, move, raycast 2d, sd raycast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
func _physics_process(delta: float) -> void: | |
player_input() | |
func player_input() -> void: | |
if Input.is_action_just_pressed("move_right"): | |
velocity = Vector2.RIGHT | |
move(velocity) | |
if Input.is_action_just_pressed("move_left"): | |
velocity = Vector2.LEFT | |
move(velocity) | |
if Input.is_action_just_pressed("move_up"): | |
velocity = Vector2.UP | |
move(velocity) | |
if Input.is_action_just_pressed("move_down"): | |
velocity = Vector2.DOWN | |
move(velocity) | |
func move(direction: Vector2) -> void: | |
var space_rid = get_world_2d().space | |
var space_state = PhysicsServer2D.space_get_direct_state(space_rid) | |
var query = PhysicsRayQueryParameters2D.create(global_position, global_position + Vector2(48, 48) * direction) | |
var result = space_state.intersect_ray(query) | |
if result: | |
if result.collider.is_in_group("Wall"): | |
return | |
position += 48 * direction | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment