Godot Version
v4.5.stable.mono.official [876b29033]
Question
The issue - projectile behaves as intended, until it encounters obstacle. It should turn itself off, eventually deal damage if encounters object that has HealthComponent and proper layer. Instead it tries to continue to move forever.
I wouldn’t be surprised if I just don’t understand layer/mask system, and I need someone to help me out here. I have project built with modular components, so I need to show everything, so please bear with me.
Physics layers:
- Environment (like terrain)
- Player
- Mobs
- Hazards
- Pickups
- Friendly Projectiles
Projectile - CharacterBody2D. Contains CollisionShape2D, DamageZoneComponent, and Area2D (DamageZoneComponent references it in code).
CharacterBody2D - layer 6, mask 1, 3.
Area2D - layer 6, mask 1, 3. (same as above)
Projectile code:
public partial class Projectile : CharacterBody2D
{
[ExportSubgroup("Nodes")]
[Export] private Area2D detectionZone;
[ExportSubgroup("Settings")]
[Export] private float speed = 200f;
private int direction;
public override void _Ready()
{
detectionZone.BodyEntered += OnBodyEntered;
}
public void Create(int direction)
{
this.direction = direction;
}
public override void _PhysicsProcess(double delta)
{
Velocity = new Vector2(direction * speed, Velocity.Y);
MoveAndSlide();
}
private void OnBodyEntered(Node2D body)
{
QueueFree();
}
}
DamageZoneComponent code:
public partial class DamageZoneComponent : Node
{
[ExportSubgroup("Nodes")]
[Export] private Area2D damageZone;
[Export] private int damageValue;
/// <summary>
/// overrides damage value
/// </summary>
[Export] private bool instakill = false;
public override void _Ready()
{
if (damageZone != null)
damageZone.BodyEntered += OnBodyEntered;
}
public void OnBodyEntered(Node2D body)
{
var healthComponent = body.GetNodeOrNull<HealthComponent>("HealthComponent");
if (healthComponent != null)
{
healthComponent.Damage(damageValue, instakill);
}
}
}
The only non-terrain obstacle that I have implemented in my dev level is an enemy (CharacterBody2D), who has CollisionShape2D and HealthComponent. CharacterBody2D of the mob is on layer 3, with mask 1. So it should work, because Projectile targets layer 3 (and 1).