|
|
|
|
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
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