[FIXED]A bullet that should not change it's direction if the player is moving [STUPID QUESTION, I KNOW]

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

Stupid question here, i know…
So… I made a top-down character which can move north, south, east and west. I want him to be able to shoot bullets in those directions. I instanced the bullet, made a simple script so the bullet goes in the direction the player faced last time. The problem is, if i move the player, it’s direction changes… the bullet’s direction changes too
(i’ve been thinking of a solution to this problem for almost 2 days… lack of exercises)
I thought of storing in a variable the direction the player faced when the bullet was shot. How can I do that?
The bullet’s movement

 motion.y = -1 * BULLET_SPEED
set_position(get_position()+motion * delta)

This is for the north direction. Basically I checked the player’s direction and with an If, I would multiply the motion’s x or y with -1 or 1 depending on the direction
I know, this is the most stupid way to do it… i’ve tried others but I was unsuccesful
Is there a better solution?

I guess you added a bullet instance under the player node.
you should add the bullet node outside of player node.

volzhs | 2019-01-03 20:52

can you add more code? perhaps having full script of player and bullet would let us help you a little more.

p7f | 2019-01-04 18:50

:bust_in_silhouette: Reply From: p7f

Hi,
You surely added bullet as child of player. Instead you should do something like this (add to root node):

func _fire():
    var bullet = preload("res://Bullets/EnergyShot.tscn").instance()
    get_node("/root").add_child(bullet)

I always use that code.

No, the bullet is the child of the main node. I used get_tree().call_deferred("add_child, bullet)
Still, if I move the player (which means changing it’s direction) the bullet changes direction too

GunPoint | 2019-01-04 08:45

And have you tried the way i wrote? Im using that in my current develpoment and works. If not, can you share some code or even the entire project?

p7f | 2019-01-04 10:51

Fixed it… finally… it was so so easy :stuck_out_tongue:

GunPoint | 2019-01-04 20:12

Great! How you did it?

p7f | 2019-01-04 20:14

check the edited post

GunPoint | 2019-01-04 20:19

You should add it as an answer and select it so others see its solved!

p7f | 2019-01-04 20:21

:bust_in_silhouette: Reply From: GunPoint

It was so damn easy… i made a function set_new_position(dir) on the bullet. Then I called it when the bullet was instanced

bullet = bulletscene.instance()
bullet.set_new_position(player_position)
get_tree().call_deferred("add_child", bullet)

Then I used the dir parameter to establish the bullet’s direction