0

Whenever I try to make a new instance of my bullet scene in Godot it would give me the typical error where I can't use .new() or .instance() I figured it out and replaced it with .instantiate() and it worked... For the most part, no errors and no warning, but when I ran my game, the bullets appeared extremely small and almost invisible and moved very slow, I know the problem is not from my bullets because they work well when I don't create them as a new scene.

I tried looking at stack overflow and even using ChatGPT but none of those sources helped. here is my bullets code:

extends Area2D
var speed = -2400
var target = position
# Called every frame. 'delta' is the elapsed time since the previous frame.
func ready():
 rotation = get_global_mouse_position().angle_to_point(position)
 
func _physics_process(delta):
 position += transform.x * speed * delta
 
func _on_body_entered(body):
 if body.name == "Dummy":
 body.queue_free()
 queue_free()

**here is my gun's code: **

extends Sprite2D
var bullet = preload("res://scenes/bullet.tscn").instantiate()
# Called when the node enters the scene tree for the first time.
func _ready():
 pass # Replace with function body.
 
func shoot():
 add_child(bullet)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta):
 rotation = get_global_mouse_position().angle_to_point(position)
 if Input.is_action_just_pressed("input_use_click"):
 shoot()
 
 

(EDITED BEYOND THIS POINT) I fixed it appearing small and moving slow now my issue is that I can only create one bullet at a time.

asked Jan 9, 2024 at 2:11
2
  • Object appearing small and moving slow sounds like the camera is showing a large area (i.e. it is zoomed out). Commented Jan 9, 2024 at 3:11
  • I fixed that and it wasn't a camera issue. Commented Jan 9, 2024 at 22:56

1 Answer 1

0

You are adding the bullets as children of the gun itself. This means that any transformation applied to the gun will be also applied to the bullets.

This can solved this by adding the bullets as a sibling instead of child. Changing add_child(bullet) to add_sibling(bullet) will do.

answered Jan 9, 2024 at 11:42
Sign up to request clarification or add additional context in comments.

2 Comments

This works for the most part but it won't let me make anymore bullets, and gives me this error message "E 0:00:01:0927 gun.gd:11 @ shoot(): Can't add child 'Bullet' to 'Node', already has a parent 'Node'. "
It's because you are instantiating only one bullet. You need to remove .instantiate() from var bullet = preload("res://scenes/bullet.tscn").instantiate() and change add_sibling(bullet) to add_sibling(bullet.instantiate())

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.