Projectile Spawn Position Issue

Godot Version

4.4.1

Question

I’m trying to make a projectile spawn at the end of the gun. this gun is always pointing at the mouse using look_at(). How do I get the information from the gun’s position and rotation?

Hi,

You need to get an access to your gun’s node, either by exporting a variable or by using a node path in your script. I suppose you already have that since you’re using look_at() on it.
Once you’ve accessed your node and stored it inside a variable (which I believe is a Node2D), you can simply get its position and rotations variables. Something like this:

@export var gun: Node2D

func some_function():
    # get
    print(gun.position)
    print(gun.rotation)

    # set
    gun.position = Vector2.ZERO
    gun.rotation = 0

From there, you can compute a position to spawn a projectile.


Another way of doing that, which is simplier, is to add a node inside your gun node, that will only act as the position of instantiated bullets.
Something like this:

You only have to access this node, and just use its position and rotation variables for your bullets, with no other computation needed.

1 Like

Oh, cool. So I just need a child node that is the position.

Well, if that works for you, I guess it’s a simple an effective way of doing that, yeah.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.