Godot 4.5
Hello, extreme Godot beginner here. I apologise if my understanding is rough.
I’m working on a 2D game that is top-down programmatically, but doesn’t use tilemaps. I am failing to get the player character (Area2D, parenting Area2D, parenting Collisionshape2D; marked in yellow at the bottom of the character) to interact with the world collision (Area2D, parenting CollisionShape2D). Both are depicted below.
Answers to similar questions either involve tilemaps, which I am not using, or physics bodies, which I don’t have any major interest in using, and attempts at using them (like, changing the player to a CharacterBody2D) result in my character behaving strangely, like flying at the edge of the screen at the press of a button, or dropping to the bottom of the screen like a rock.
The code for my player is essentially as follows, largely copied from the 2D game tutorial:
extends Node2D
var speed = 250 # Movement speed in (pixels/sec).
func _ready() -> void:
screen_size = get_viewport_rect().size
func _process(delta: float) -> void:
## This controls movement.
var velocity = Vector2.ZERO # The player's movement vector.
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)
The Area2D that actually contains the player CollisionShape2D has no code, as does the Area2D containing the world collision.
I recognise that the Area2D that actually contains the CollisionShape2D within the player scene should contain code that detects the collision with the world CollisionShape2D, and communicates the details to the player Area2D, though I have absolutely no clue how to make this work, nor what exactly the parent should do with this information programmatically.
If anyone could help, I would appreciate being walked through this like I’m five. I’ve been able to do pretty well with systems in Godot that don’t seem to have any sort of confusing built-in functionality, but where collision is concerned I may as well BE five.
Thank you!
