apply_impulse function not applying impulse to RigidBody2D "bullet" instance with desired rotation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheJuJuTrain

So I’m in the process of making a top down shooter and am trying to implement a weapon system where all the weapon functions are handled from a separate “weapon” node that gets instanced as a child of the player node in the world scene. When I was first learning how to use this engine, I initially handled weapon shooting through the player node script, but I soon realized that this would make the player script ridiculously long and would make it difficult to have more than one weapon, so I decided to try this separate weapon node that I am now trying to work out.

Everything worked fine when I had everything in the player node script, but now that I have it in the separate weapon node script, bullets don’t shoot in the direction of the mouse pointer anymore.

Here is the code that handles the gun firing in my weapon node, which is identical to how it was originally in the player node:

var bullet = preload("res://objects/bullet.tscn")
#this bullet object is a RigidBody2D node

... #irrelevant code in between

func shootGun():

	... #irrelevant code in between

	for i in (bulletsPerShot):
		var bulletInstance = bullet.instance()
		#bulletPoint is the barrel of the gun
		bulletInstance.position = $bulletPoint.get_global_position()
		bulletInstance.rotation_degrees = rotation_degrees
		bulletInstance.apply_impulse(Vector2(), Vector2(BULLET_SPEED, 0).rotated(rotation))
		get_tree().get_root().add_child(bulletInstance)

The bullet only shoots to the right, no matter what the bulletInstance.rotation is set to, but the position that it’s supposed to shoot from is correct.
I thought at first that the weaponNode wasn’t rotated correctly when childed to the player node, so I tried having it print what the value of bulletInstance.rotation_degrees was with every time the shootGun() function is called, but I found that the value is actually updating every time, it’s just the apply_impulse seems to ignore this rotation and I don’t understand why :frowning:

For more context, here is the relevant part of my weaponNode tree:

weaponNode
 | -v pistol
 |   | -> bulletPoint

And here’s the relevant parts of the world scene:

world
 | -v player
 |    | -> weaponNode
:bust_in_silhouette: Reply From: TheJuJuTrain

I figured it out.

the line:

bulletInstance.apply_impulse(Vector2(), Vector2(BULLET_SPEED, 0).rotated(rotation))

needed to be:

bulletInstance.apply_impulse(Vector2(), Vector2(BULLET_SPEED, 0).rotated(global_rotation))

because the node changes global_rotation in the world scene as I controlled the player rotation with mouse movement, but the node’s base rotation value was not set to change in the same, so it had the default rotation of 0 no matter what.