0 votes

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.rotationdegrees was with every time the shootGun() function is called, but I found that the value is actually updating every time, it's just the applyimpulse seems to ignore this rotation and I don't understand why :(

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
Godot version 3.5.1
in Engine by (19 points)

1 Answer

0 votes
Best answer

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.

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.