There are a few things I'd try based on the GIF.
The first thing I'd check is to see if the target position is where you expect it to be. I would make a MeshInstance node attached to the weapon called something like DEBUG
and have it be a small, brightly colored sphere that has good constrast with the environment. Then I'd do something like get_node("DEBUG").global_transform.origin = target
in the code for the weapon so you can see if the target position is where you would expect it to be. You could also check to make sure the direction is pointing in the correct direction the same way. I think the target position and direction should be correct, but I have found that double checking may reveal that something minor is off in the calculation.
The second thing I'd do is temporarily comment out the _integrate_forces
function in the bullet, and instead add something like this to it's _ready
function:
func _ready():
apply_impulse(Vector3.ZERO, direction)
That would help tell if the impulse being applied each time _integrate_forces
is being called is causing the issue or not. Another thing to try is locking the rotation of the RigidBody, as while I think the apply_impulse
function isn't dependent on the rotation of the node, it might be and that may explain why the bullet is veering off to the side.