Problem With Player not Colliding With Tilemap

Godot Version

4.5

Question

Hello! I’m really new to Godot, I’m sorry if my understanding is really bad. I’ve created and placed my tilemap, along with my player with their collision box, but every time I play the scene, my player just falls down past the map as though there’s no ground. All I have in my scene is the player, the player sprite, a camera and the placed tiles. (This is a top-down game, if it helps!)

Small Update: Ive got my player on top of the tilemap now, but the player isnt able to move. Here’s my script:

extends CharacterBody2D

var direction : Vector2 = Vector2()


func read_input():
	velocity = Vector2()
	
	if Input.is_action_pressed("Up"):
		velocity.y -= 1
		direction = Vector2(0, -1)
	if Input.is_action_pressed("Down"):
		velocity.y += 1
		direction = Vector2(0, -1)
	if Input.is_action_pressed("Left"):
		velocity.x -= 1
		direction = Vector2(0, -1)
	if Input.is_action_pressed("Right"):
		velocity.x += 1
		direction = Vector2(0, -1)
		
	velocity = velocity.normalized()
	
	
func _physics_process(delta):
	read_input()

You need to call move_and_slide in order for the velocity to be used in collisions.

1 Like

I added move and slide, and it still hasn’t worked? Am i missing something? (I just added it in the bottom, am i supposed to add it somewhere else?)

Did you add it in the _physics_process() function?

Yes, I did now, it still isn’t letting me move

Earlier you said that the player falls down. Is it no longer doing that?

No, sorry, I thought i put that in the update ebfore you responded :). Its not falling down anymore, its just stuck.

Did you create the “Up”, “Down”, “Left” and “Right” actions in the input map? Did you assign the expected buttons to the actions?

1 Like

Yep ^-^

It still isn’t working, but it seems like the player is glitching down and up between one pixel? Like when you glitch out of bounds in nintendo games

Maybe your velocity is just too small to notice? You have it set to a length of 1.

1 Like

I switched it to ten, it still isnt working. I might just have to start a new project :,D (thank god i didnt get too deep into it) thank you so much though!!

Actually, now I think its my script. I started another tutorial to try and just get the code, and its acting weird

I have an error saying “he parameter “delta” is never used in the function “player_movement()”. If this is intended, prefix it with an underscore: “_delta”.”

Is it a preset thing that I have? This is a completely new file, so now its not the tilemap

extends CharacterBody2D

const speed = 100

func _physics_process(delta):
player_movement(delta)

func player_movement(delta):

if Input.is_action_pressed("Right"):
	velocity.x = speed
	velocity.y=0
elif Input.is_action_pressed("Left"):
	velocity.x = -speed
	velocity.y=0
elif Input.is_action_pressed("Down"):
	velocity.y = speed
	velocity.x=0
elif Input.is_action_pressed("Up"):
	velocity.y = -speed
	velocity.x=0
else:
	velocity.x = 0
	velocity.y = 0

move_and_slide()

That’s not an error but a warning. It’s just telling you that you aren’t using the variable anywhere so it should start with an underscore as per Godot’s coding style guidelines. You can ignore the warning if you want.

oooh okay. I just thought it was weird because the guy in the tutorial didn’t have it

(How to Create an RPG in Godot 4 (step by step) by DevWorm is the tut im referencing by the way :D)

1 Like

Assuming you are making a top down game, you have two problems, one you didnt use move_and_slide to fix that I reccomend using this script:
extends CharacterBody2D

const SPEED := 115

func _physics_process(_delta: float) → void:
var direction := Vector2.ZERO

if Input.is_action_pressed("left"):
	direction.x -= 1
if Input.is_action_pressed("right"):
	direction.x += 1
if Input.is_action_pressed("up"):
	direction.y -= 1
if Input.is_action_pressed("down"):
	direction.y += 1

direction = direction.normalized()
velocity = direction * SPEED
move_and_slide()

problem two, is your tilemap. your tiles need collision. to do this, you will need to:
1: Select Tilemap Node
2: In the inspector on the right select “physics layers” (it may appear as layers cuz im on v 4.6 but idk)
3: under physics layers, click add element.
4: in the bottom, select “TileSet”
5: select “paint”
6: in the “select property from editor” bar, select “Physics Layer 0”
7: Click a tile on your tilemap to give it its hitbox.
8: make sure your players hitbox is in collisionlayer 1.
9: test and it should work

:slight_smile: hope it works, and good luck programing

1 Like

I put your code in ^-^ it seems like its giving me in error though

“Error at (6, 38): Mixed use of tabs and spaces for indentation.”

The error “Mixed use of tabs and spaces” means that on that line of code, you likely used a few spaces to indent the code, while the rest of your script uses tabs (or vice versa). It just means your code works fine, you accidentaly mixed indentation types. Hope your game turns out well!

I put collision on my tiles now :smiley:

But when i put in the code (below), it says direction isn’t identified in current scope?

extends CharacterBody2D


const SPEED := 115

func _physics_process(_delta: float) -> void:
	var direction := Vector2.ZERO
	
func read_input():
	velocity = Vector2()
	if Input.is_action_pressed("left"):
		direction.x -= 1
	if Input.is_action_pressed("right"):
		direction.x += 1
	if Input.is_action_pressed("up"):
		direction.y -= 1
	if Input.is_action_pressed("down"):
		direction.y += 1

	direction = direction.normalized()
	velocity = direction * SPEED
	move_and_slide()

xenoshrimp messed up the copy/pasting code, you need to have this line as well

var direction := Vector2.ZERO

However, the movement code can be made way shorter.

extends CharacterBody2D

const SPEED := 115

func _physics_process(_delta: float) -> void:
    velocity = Input.get_vector("left", "right", "up", "down") * SPEED
    move_and_slide()

The Input.get_vector() function will get all four inputs for you. It also limits the length of the vector to 1 as well, so you don’t need to normalize it.

1 Like

Not super related but YO I LOVE HIS TUTORIALS!

1 Like