Godot Version
4.3
Question
I’m making a game similar to Bomberman. I have a level that has a tile map layer,
and that layer has three tiles - floor, unbreakable walls and breakable walls. I made an enemy that shoots. His projectiles should collide with walls and disappear, but sometimes projectiles go through walls.
I check if the projectile collides with walls by using OnBodyEntered signal of the attached Area2D and checking if atlas coordinate of projectile’s position doesn’t correspond to a floor tile:
public partial class Projectile : Node2D
{
public Vector2 direction;
public override void _Ready()
{
Area2D area2D = GetNode<Area2D>("Area2D");
area2D.BodyEntered += body =>
{
if (body.Name == "Bomberman")
{
Bomberman bomberman = body as Bomberman;
bomberman.Die();
var sound = GetTree().GetRoot().GetNode<Node2D>("Level").GetNode<AudioStreamPlayer2D>("Shooter");
sound.Play();
QueueFree();
}
else if(body is TileMapLayer)
{
Walls walls = body as Walls;
Vector2I mapPos= walls.LocalToMap(ToGlobal(Position));
Vector2I atlas= walls.GetCellAtlasCoords(mapPos);
if (atlas != Walls.Floor)
{
var sound = GetTree().GetRoot().GetNode<Node2D>("Level").GetNode<AudioStreamPlayer2D>("Shooter");
sound.Play();
QueueFree();
}
}
};
}
public override void _PhysicsProcess(double delta)
{
Position += direction;
}
}
Help me to debug this or tell me if there’s a better way to handle this.