How to shoot Bullets

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

I am very confiused about Bullets. I want to make a KinematicBody2D, who can shoot Bullets. I saw lots of tutorials about it, but I dont get it. Can somebody tell me how to do it?

The forum isn’t really a good place to explain a concept like shooting bullets. Really, tutorials (written or video-based) are a much better outlet for that sort of content.

  • What tutorials have you watched?
  • What part(s) are causing the confusion?
  • What have you tried?
  • Did you get errors? If so, what were they?

jgodfrey | 2020-02-14 21:49

:bust_in_silhouette: Reply From: Newby

For bullets I use an Area2D with a sprite and make it a separate scene this is where you put how the bullet will fire. Then i add a Position2D on my player and call the bullet scene. Note that the Postion2D is optional i just use it cause i dont want my projectiles spawning from my player position.

Bullet

func _physics_process(delta):
    position += 10 //this is to make the bullet fly to the right when fired

Player

onready var BULLET = preload("Bullet Path")
func _process(delta):
    if Input.is_action_just_press("Button of your choosing")
       var bullet =  BULLET.instance
       get_node("/root/World").add_child(bullet)
       bullet.global_position = $Postion2D.global_pisition

when adding the bullet to the scene make sure it is not a child to the one who fires it cause if the parent will cange position or rotation the bullet will to in my example the nodes are placed like this.

Worrld
-Player
–Position2D

For tutorials i recomend watching HeartBeast on projectiles. If you need me to explain anything else just ask.

How can I conect the Position2D with the Bullet?

Godot_Starter | 2020-02-16 11:54

You dont need to connect the vullter to the Position2D, it is used as a reference postion for where the bullet will spawn, just add a script to your player like the example above, the Position2D should be a child of the player. When you instance the bullet.
Shown with my example above

get_node(/root/World).add_child(bullet)

This will spawn in the bullet now you have to set its position i use Position2D as a reference where the bullet position will be when it spawns in.

bullet.global_position = $Position2D.global_postion

note “$” is a shortcut way for get_node()

The example im using uses a scene tree that looks like this
World
-Player
–Position2D

Newby | 2020-02-16 13:23

Now i understand what the Position2D Node is doing. Thanks for that! But I tried to copy your bullet solution, but I got this error in the player script:

Invalid get index ‘global_position’(on base:‘null instance’).

Do you now my mistacke?

Godot_Starter | 2020-02-16 17:24

There are some bugs in the code that was given
First is here.

bullet.global_position = $Postion2D.global_pisition

Second is here.

bullet.global_position = $Position2D.global_postion

On each piece of code the second global_position is spelled wrong

on the first piece it’s global_pisition

then second is global_postion

I made the same mistake on a project I was learning with.
word for word on the second one lol.

These are things I just noticed because it happened to me recently.
Checking code is hard. Making games is fun