0 votes

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
in Engine by (15 points)
edited by

1 Answer

+1 vote
Best answer

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()
by (330 points)
selected by

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

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.

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.