|
|
|
|
Reply From: |
DigitalDrako |
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()
muzzle.add_child(bullet)
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"):
fire_bullet()
func fire_bullet():
var main = get_tree().current_scene
var instance = bullet.instance()
main.add_child(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()
bulletTimer.start()
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!
It worked! Thank you very much!
jmmarzon12 | 2020-06-21 08:37
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?
jmmarzon12 | 2020-06-22 00:04
Yeah, I’d love to! Could you show me your code? And did you use the first example or the second?
DigitalDrako | 2020-06-22 01:57
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.
DigitalDrako | 2020-06-22 03:05
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).
THANK YOU SO MUCH!
jmmarzon12 | 2020-06-22 07:33
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()
main.add_child(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()
DigitalDrako | 2020-06-22 16:48
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)
jmmarzon12 | 2020-06-23 00:00
Thank u very much !
Kushal | 2021-11-16 16:33