Including player velocity in bullet velocity

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I am running into a problem with firing bullets. I have a player that is a rigidbody2d and a gun that fires another rigidbody2d. If the initial linear_velocity of the projectile is high, then it is fine. But if it is too low and the player is moving fast the player overtakes the projectile. When I generate the projectile I set its linear_velocity based on the direction aimed * projectile speed. Then I add the player’s linear_velocity to it. But I still overtake the projectile. Is there a better way of including in the parent velocity in the projectiles velocity?

func shoot( direction, inherited_inertia):
	_life = 0
	var target_velocity = direction * projectile_speed
	if(inherits_inertia):
		target_velocity += inherited_inertia
	$RigidBody2D.linear_velocity = target_velocity 
	$RigidBody2D.contact_monitor = true
	_in_flight = true

Should I apply the parent’s velocity as a force? Or an impulse? Or something else to kick the projectile faster?

Your best bet could be applying the impluse to the projectile, if that doesn’t work you could try to multiply the players velocity(inherited_inertia) and target velocity, which means switching += for *=.

In theory both aproaches could work

Should

if(inherits_inertia):

be

if(inherited_inertia):

1 Like