I keep having issues with collisions

Godot v.4.2.1

Hi there!
I’m still a rookie at coding, and I’ve got some problems with the collisions. I’ve seen some videos of people checking collisions with the move_and_slide(var) command, but when I try to, an error pops out saying that there’s no variable expected inside this. I also tried with the move_and_collide(var), but somehow it doesn’t work well, my characterbody2d doesn’t collide with anything. Any suggestions?

Here’s the script I used:

func _physics_process(delta):

#setting the player's velocity
if Input.is_action_pressed("move_right"):
	Velocity.x = speed
if Input.is_action_pressed("move_left"):
	Velocity.x = -speed
if Input.is_action_pressed("move_down"):
	Velocity.y = speed
if Input.is_action_pressed("move_up"):
	Velocity.y = -speed
move_and_collide(Velocity)

move_and_slide has been changed in godot 4. You dont need a variable anymore:

#setting the player's velocity
if Input.is_action_pressed("move_right"):
	velocity.x = speed
if Input.is_action_pressed("move_left"):
	velocity.x = -speed
if Input.is_action_pressed("move_down"):
	velocity.y = speed
if Input.is_action_pressed("move_up"):
	velocity.y = -speed
move_and_slide()

Try this.
It will use the built-in velocity-variable automatically

1 Like

I tried to do as you said, but my collisions won’t work! Thanks for the information about the updates in godot 4, but I guess there are some things that I am still missing on the issue

Are your static bodies on a different collision layer? Are you working on upgrading an old project?

Can you paste your new script, make usre to use proper formatting

2 Likes

Hi! As I said before, I’m a rookie at coding, and I was trying something new, so no, it wasn’t an old project. Tysm for trying to help, your comment made me remember something about the collision layer that I heard and I managed to solve the problem on my own ^^

1 Like