The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

So i have a bullet scene which is a Area2D node, and i want to instance it into my Muzzle(refer to the 2nd Picture) but i don't know how to.. pls help me thanks :DPicture1

in Engine by (18 points)

2 Answers

+1 vote
Best answer

Your code seems correct, I think the issue is arising with the fact you're preloading your Bullet scene in the same line that you're instancing it. Since it's being immediately instanced, the "preload" isn't able to load it before hand. Here's how I would do it:

const Bullet = preload("res//:Bullet.tscn")

func _process(delta):
     if Input.is_action_just_pressed("fire"):
     var muzzle = get_node("Muzzle")
     var bullet = Bullet.instance()
     bullet.global_position = muzzle.global_position
     insert your other variables here

From a tutorial project however, I used this code:

const Bullet = preload("res//:Bullet.tscn")

func  _process(delta):
   if Input.is_action_just_pressed("fire_bullet"):

 func fire_bullet():
    var main = get_tree().current_scene
    var instance = bullet.instance()
    instance.global_position = muzzle.global_position
    bullet.velocity = Vector2.RIGHT.rotated(gun.rotation) * BULLET_SPEED
    bullet.velocity.x *= sprite.scale.x
    bullet.rotation = bullet.velocity.angle()

the reason why the bullet is instanced as a child of the main scene as opposed to the gun is so the bullet moves independently of the gun. If you instance the bullet as a child of the gun, you would soon find the bullet would move when the gun moved due to the nature of children in Godot.

Hope this helps!

by (207 points)
selected by

It worked! Thank you very much! :D

I've encountered a problem with the solution you gave me sir, and it is about when i press fire at a certain angle my bullet goes the other direction or it goes 90/-90 degrees to where i am clicking (oh and btw i scripted my gun to look where my cursor is at), can u help me with my problem?

Yeah, I'd love to! Could you show me your code? And did you use the first example or the second?

Hey I built a test project using the code provided, if you remove the line bullet.velocity.x *= sprite.scale.x it should work. The reason this happens is because when the bullet is going left (and has a negative velocity) the player is facing left (a negative scale.x) so multiplying the two together makes sure the velocity always moves in a positive direction. This worked just fine in my example project, though honestly, I'm not entirely sure why it's there. It probably has something to do with flipping the bullet sprite.

Hey i just woke up!
1.) Sorry i cant show you the code yet because my pc just broke down for some reason. im sorry ill show you when its fixed (they said its gonna take atleast 5hours to fix due to the number of customers in their shop).
2.) I used the 1st example of code you sent me.
3.) about this -> (bullet.velocity = Vector2.RIGHT.rotated(gun.rotation) * BULLET_SPEED), do i need to make a variable which is a Vector2()?
4.) can u explain the 2nd example of code to me? (line by line, not that i dont understand everything just for some clarification)(and if its too much of a bother, u dont need to do it and skip this).

No worries, I like answering questions.

To answer your third question: I added a script to my bullet scene that stored the variable

velocity = 0

And to answer your fourth:

func fire_bullet():
    # We get the current scene as opposed to the muzzle so when the bullet is instanced it moves independently of the muzzle.
    var main = get_tree().current_scene
    var instance = bullet.instance()
    # Here we set the position of the bullet to the muzzle
    instance.global_position = muzzle.global_position
    # This sets the velocity of the bullet based on the rotation of the gun. 
    #BULLET_SPEED is a variable
    bullet.velocity = Vector2.RIGHT.rotated(gun.rotation) * BULLET_SPEED
   # This rotates the bullet sprite to match the direction it's moving in.
    bullet.rotation = bullet.velocity.angle()

So i've encountered yet another problem, the bullet stay as the same place with my muzzle and does not shoot/go to another position. How do i fix that?(Refer to picture1)

So i've encountered yet another problem, the bullet stay as the same place with my muzzle and does not shoot/go to another position. How do i fix that?(Refer to link1)

Thank u very much !

0 votes

Instance Bullet scene as a "Packed scene" .refer :- Packed scene

by (51 points)

how do i exactly do that sir? im sorry i dont quite understand the tutorial about packedscene

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.