Having problems with tilemap collisions. The map doesn't recognize them

Godot Version

4.0.2 stable

I made various tile maps in my game. I’m making an RPG where the character just moves around. Its 2D top-down. What I want is that the player collides with walls and objects. Since I’m just using a old fashioned tile map I just configured the collisions that come with tile maps.

I already painted the collisions on the boxes. Most walls I did them. but when I run the scene the player can run over walls. I also set up the collision masks for the player and the world, on both the tile-map and the player. I tried uploading images of my game.

What does your player movement script look like? Are you using move_and_slide?

1 Like

No. I’m not using it. I’m using a CharacterBody2D.

extends CharacterBody2D

#how fast the player moves
@export var speed : float = 200.0
@onready var _animated_sprite = $AnimatedSprite2D
var _last_direction := Vector2.ZERO

var screen_size
Called when the node enters the scene tree for the first time.
func _ready() → void:
screen_size = get_viewport_rect().size

func _physics_process(_delta):
if Input.is_action_pressed(“ui_left”):
velocity.x = speed * -1
elif Input.is_action_pressed(“ui_right”):
velocity.x = speed
elif Input.is_action_pressed(“ui_up”):
velocity.y = speed * -1
elif Input.is_action_pressed(“ui_down”):
velocity.y = speed
velocity = Vector2.ZERO

if velocity != Vector2.ZERO:
_last_direction = velocity

play appropriate animation

if velocity.x > 0:
elif velocity.x < 0:
elif velocity.y > 0:
elif velocity.y < 0:
# velocity is zero, so play idle animation based on last direction

if velocity.length() > 0:
velocity = velocity.normalized() * speed

position += velocity * _delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)

func play_idle_animation():
if _last_direction.x > 0:
if _last_direction.x < 0:
if _last_direction.y > 0:
if _last_direction.y < 0:

I just added Move_and_slide() to my script and it worked. Its detecting the collisions. Thanks.

1 Like

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