Projectile problem

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

can someone help me on how to create a projectile?
Currently, the projectile when pressed tab “shoots out” and goes to the right properly but not in the location respective of the player. It only shoots from one point on the map. I want it to be able to follow the player and wherever he is to shoot from there. How do I do this?
The code at the bottom of actionpressed is where I need help. As you can see, when I comment out that line of code the fireball appears and goes to the right in a set position on screen. But I want it to come out of my player depending on the players position, so I added in the (hashtagged out) line of code but when I do so the fireball does not appear.

func _physics_process(delta):

if Input.is_action_pressed("ui_right"):
	velocity.x = SPEED
	$AnimatedSprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
	velocity.x = -SPEED
	$AnimatedSprite.flip_h = true
	velocity.x = 0
	if on_ground == true:

if Input.is_action_pressed("ui_up"):
	if on_ground == true:
		velocity.y = JUMP_POWER
		on_ground = false

if Input.is_action_just_pressed("ui_focus_next"):
var fireball = FIREBALL.instance()
#fireball.position = $Position2D.global_position
:bust_in_silhouette: Reply From: Sprowk

I’m guessing your projectile is another scene and you are instancing it. The thing you forgot is to change the location of the bullet before it is added to the tree.

var bullet = preload("res://scenes/bullet.tscn")
func shoot(pos):
	var b = bullet.instance()

Thanks that does help, I do have a question with my code, I’ll put it on here. I’m not sure how to change it

Xalian | 2019-10-28 05:00

I never really coded in Godot 3 but there are 2 things that could cause that. Firstly, you set a position with a value outside your camera. Secondly, setting position of physics object is usually not good as these things handle the physics engine. You can avoid that by setting the position before adding it in the tree. tldr: move up the commented line.

Sprowk | 2019-10-28 14:52

Ah I see, thanks for the help! I’ll make those changes, but for your first one which you said it could be assigned out of the camera, how do I prevent that?

Xalian | 2019-10-28 16:40