blob: e613849ddb720838135a73d22fe1a6a42c993408 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using Godot;
public partial class Bullet : Area2D
{
[Export] public float Speed { get; set; } = 600.0f;
public Vector2 Direction { get; set; } = Vector2.Zero;
public override void _Ready()
{
// Subscribe to the built-in BodyEntered event when the bullet is spawned
BodyEntered += OnBodyEntered;
}
public override void _PhysicsProcess(double delta)
{
Position += Direction * Speed * (float)delta;
}
// This method is triggered automatically when the bullet hits a physics body
private void OnBodyEntered(Node2D body)
{
// QueueFree safely deletes this node and all its children at the end of the current frame
QueueFree();
}
}
|