Godot Version
Godot 4.3
Question
Hello, I’m working on a 2D platform game and I have my main character made with CharacterBody2D, and monsters made with it too. I move both with move_and_slide() and check collisions with get_slide_collision()
I have this monster that moves horizontally toward my player, if it collides with him the collision is normally detected, but if the player is touching a wall during the monster collision, this collision is not detected and my player is “pushed” inside the wall by the monster and the player disappers afterwards.
Why the collision is not detected if a CharacterBody2D is touching a wall?
This is a blocking problem, please help!
Thank you
Please share the code that detects when the monster collides with the player.
func _physics_process(delta: float) -> void:
move_and_slide()
for i in get_slide_collision_count():
var collider = get_slide_collision(i).get_collider()
if (collider.name == "GameMap"):
if (_get_tiletype_on(collider) == "spikes"):
die()
if collider.name == "Diamond":
is_winning = true
$WinSound.play()
player_win.emit()
if collider.is_in_group("moving_platforms"):
if (position.y < (collider.position.y-38)) && is_on_floor():
collider.set_active()
# This is the problem: if my player touches a wall this collision with the CharacterPlayer2d of the monster is not triggered!
if collider.is_in_group("monsters") || collider.is_in_group("spits"):
die()
I created a little example project:
In this project there are two CharacterBody2D: player and monster. Monster moves on the right, if the player is not touching the wall on the right the collision with CharacterBody2D is detected.
But if you move the player on the right making it touch the wall and wait for the monster to arrive, the collision with the monster is not detected.
I don’t know if this is a problem of Godot or I’m doing something wrong, but it’s something weird that I’d like to understand.
Thank you!
use an Area2D
for detecting enemies and move the detection code there.
and set the player, enemy and world to different physics layers
to prevent the player being pushed. you can code this instead into an animation when the player is hit by the enemy, and you can prevent the glitchy behaviours. it could also allow the player to go through the enemy.
These are good suggestions, thank you! But I keep wondering if a collision is skipped under certain circumstances, isn’t that strange behavior anyway.