Position2D that translates with player for TDS for gun barrel?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AveryRe
:warning: Old Version Published before Godot 3 was released.

My code is here: https://github.com/averyre/AveryTDS

Basically, I want the bullet to fire from the players gun barrel (right now it just fires from the centre of the player). I created a Position2D child node and lined it up where I needed it relative to the parent node but it doesn’t move nor rotate with its parent, the player.

Help would be appreciated!

:bust_in_silhouette: Reply From: ericdl

The code that tells the player to move and look at the mouse is only affecting the player sprite. A simple fix would be to create a proper physics body for your player and put the player sprite, camera, and Position2D as children of that physics body. Then instead of using self.look_at(mousePosition) to rotate the sprite, use self.set_rot((mouse_Position - get_global_pos()).angle())to rotate the physics body and the Position2D will follow.

I threw together a quick example project using your sprites which illustrates the fix: https://drive.google.com/open?id=0BwnfZQAEnciAMmdCS0RrSkZQT1U

Thanks for the response! I would open your example project but for some reason the link wont work for me. The code is working, I look at the mouse and all but when I try to fire the bullet using bullet.set_pos(get_node("ShotPoint").get_global_pos()); for the position it returns Attempt to call function 'get_global_pos' in base 'null instance' on a null instance. now D:

AveryRe | 2016-10-27 17:07

Nevermind! Got it working. Thanks!

AveryRe | 2016-10-27 17:15