• Godot Help
  • Fire bullets untethered to the current position of the player

Hey there all! so I'm currently stuck and any help would be appreciated.

So im making a top down shooter and im trying to get the bullets to not track with the players turning. currently what they are doing is essentially firing in a straight line, and whenever the player turns the bullets turn to match their movement.

I want the bullets to remain at their set firing position whenever the player rotates so they stay on their intended path.

Below is the code im using to accomplish what im already doing:

extends CharacterBody2D
#free floating variables - variables not contained in a func or if statement
var bullet_speed = 2000
var bullet = preload("res://bullet.tscn")
var shellcasing = preload("res://shellcasing.tscn")


#All code below is functional
var movespeed = 500

func restart():
	get_tree().reload_current_scene()

func _process(_delta):
	if Input.is_action_just_pressed("escape"):
		get_tree().quit()
	if Input.is_action_just_pressed("levelrestart"):
		restart()

func _ready():
	pass
func _physics_process(_delta):
	global_rotation = global_position.direction_to(get_global_mouse_position()).angle() + PI/2.0
	var move_direction = Input.get_vector("left", "right", "up", "down")
	velocity = move_direction * movespeed
	move_and_slide()

#Below is a test fire function called func Shoot():
	if Input.is_action_just_pressed("shoot"):
		shoot()
	

func shoot():
	var s = shellcasing.instantiate()
	add_child(s)
	s.transform = $Ejector.transform
	
	var b = bullet.instantiate()
	add_child(b)
	b.transform = $Muzzle.transform
	
	#Below is the code that the tutorial said to use, but It did nothing
	
	#var bullet_instance = bullet.instantiate()
	#bullet_instance.position = get_global_position()
	#bullet_instance.rotation_degrees = rotation_degrees
	#bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed,0).rotated(rotation))
	#get_tree().get_root.call_deferred("add_child",bullet_instance)

I was following a few seperate tutorials, and one of them said to simply set the bullets using a .transform_global command which didnt work.

Im still very new to designing games in godot, and in general so any help anyone can give would be awesome! If you need anything clarified please let me know.

  • xyz replied to this.
  • So for everyone out there, my buddy Syber (the GOAT) helped me out with this one:

    var b = bullet.instantiate()
    get_tree().current_scene.add_child(b)
    b.global_transform = $Muzzle.global_transform

    for my particular code above, with a bit of help for XYZ, I just needed to add get_tree().current_scene.add_child(b)
    and it works like a charm. hope this helps someone else in the future!

    TheFollyWorks Don't parent bullets to the player. Because if you do, they'll obviously inherit and "follow" player's transforms

      xyz
      Ah ok, so essentially because I have them written in this way, its causing the rotation to be based off of the players movement.

      ok, so how would I unparent them in this particular example? I mentioned the .transform_global, but that didnt work for me. is there a way to easily do this?

      • xyz replied to this.

        TheFollyWorks The catch is in add_child(b) call. This method adds the node as a child of an object it's been called on. If you call it from a script that's attached to the character body node, it will parent it to that node. So you need to do: some_other_node.add_child(b). And what exactly is this some_other_node, that depends on your scene setup. It typically would be a top node in the level scene or possibly a scene tree root node. Can't give you specific suggestions since you haven't posted your overall scene structure. For start you can try with player's parent node: get_parent().add_child(b)

          xyz so I tried get_parent() and it disconnected the bullets from the player, but they dont fire out of the muzzle and instead remain in a fixed position.

          currently my main node consists of just the main node, which is called world and the player node. see below:

          the player node is broken up into a bunch of smaller nodes. See below:

          Im essentially trying to get it to fire out of the muzzle whilst having the trajectory of the bullet remain the same. Hope this helps for further clarification!

          • xyz replied to this.

            TheFollyWorks b.global_transform = $Muzzle.global_transform
            You'll also need some code to move the bullet.
            Post the bullet scene structure as well.

              xyz

              and if someone were to write that out what might that look like, as im not quite sure what you mean here.

              • xyz replied to this.

                xyz respectfully, I have. The first tutorial I watched and followed turned out to be for 3.X so it wasnt all applicable. The next tutorial I read worked for them, but when I tried to add it to the project I was doing, It didnt work. thats what I meant when I said I had followed a few tutorials.

                So again, respectfully, I would appreciate it if you could explain to me what you mean by this

                "Ok, so you need to move the bullet in the script that's attached to the bullet"

                • xyz replied to this.

                  TheFollyWorks You need to implement some code that moves the bullet. Do you have such code implemented in your bullet scene? There's a script attached to the top node in the bullet scene. What's that script for? Is it for bullet movement?

                  So for everyone out there, my buddy Syber (the GOAT) helped me out with this one:

                  var b = bullet.instantiate()
                  get_tree().current_scene.add_child(b)
                  b.global_transform = $Muzzle.global_transform

                  for my particular code above, with a bit of help for XYZ, I just needed to add get_tree().current_scene.add_child(b)
                  and it works like a charm. hope this helps someone else in the future!

                  I literally just got done figuring all of this out for a top down helicopter game.

                  1) From the player input script I sent a signal to the main game script with the location and position of the place the bullet spawns.

                  2) The main script handled the instantiating the bullet.

                  3) The bullet handled it's forward movement in it's own script.

                  At first I thought this was a bit of overkill just to shoot a bullet. After thinking it through though, it makes a lot of sense.