I am creating a 2D shooter using Godot mono, and I'm fairly new to this engine. I have a KinematicBody2D player scene that instantiates an Area2D bullet:
// Player.cs
public void FirePhaser(PackedScene phaserBurst, AnimatedSprite sprite)
{
var burst = (Area2D)phaserBurst.Instance();
AddChild(burst);
// Helper method for flipping the bullet sprite
SetPhaserBurstHorizontalDirection(burst, sprite);
}
This works great and the bullet travels as expected:
// Bullet.cs
public override void _Process(float delta)
{
float speed = 1500;
float movement = speed * delta;
AnimatedSprite sprite = GetNode<AnimatedSprite>("PhaserBurstSprite");
if (sprite.FlipH)
{
Position -= Transform.x.Normalized() * movement;
}
else
{
Position += Transform.x.Normalized() * movement;
}
sprite.Play("fire");
}
...until my Player moves. If my player moves in the same direction of the bullet, it moves faster. If the player moves away from the fired bullet, it slows down. It seems pretty obvious to me that since I am adding the bullet as a child of the player, the velocity changes of the player are factoring into the overall movement speed of the bullet. Before I go about writing any kind of verbose logic to adjust the equations, is there a better way of decoupling the Player's velocity from the Bullet? I'm happy to provide any more context or code needed. Thanks in advance!