Collisions not working

Godot Version

Godot v4.2.2

Question

Hi,

i followed every tutorials i found on the web but i sti have the same issuer over and over: collision is not working.

I create a character body 2d, then a sprite, then a collision shape. I create a static body then a collision shape to but the player he’s still passing through the area.

Is there something a miss there?

I’ll really appreciate some help with my begining in Godot.

Thx
Regards

1 Like

You need to show your code, also need to show how the collision layers/mask are setted

ok,

i just can’t see on tutorial what kind of code the collisions need.

you can find the files here if you have some time

And one more thing: i’m using blockcode

You are modify the position of the CharacterBody2D. This bypasses the physics engine, which handles collisions. Instead you should use move_and_collide() or move_and_slide() methods.

Your code generate with BlockCode:

extends CharacterBody2D


func _ready():
	pass

func _process(delta):
	if (Input.is_action_pressed('ui_right')):
		position += Vector2(5, 0)
	if (Input.is_action_pressed('ui_left')):
		position += Vector2(-5, 0)

It can be like this:

extends CharacterBody2D

var speed: float = 200.0

func _process(delta):
    var motion: Vector2 = Vector2.ZERO

    if Input.is_action_pressed('ui_right'):
        motion.x += 1
    if Input.is_action_pressed('ui_left'):
        motion.x -= 1

    motion = motion.normalized()

    velocity = motion * speed
    move_and_slide()
1 Like

Thx, i understand that some blocks don’t appear when it’s a simple strip or a charcater body.

You’re answer helpe me.