i have a bullet spawn problem?

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

I have a player scene and a world scene however, when I shoot bullets in the player scene they spawn in their intended destination, when I shoot in the world scene they spawn in the bottom right. how do I fix this?
here is the code for the player(the only script I’ve made so far):

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

const bullet_path = preload("res://bullet.tscn")

func get_input():
	velocity = Vector2()
	if Input.is_action_pressed('down'):
		velocity = Vector2(-speed, 0).rotated(rotation)
	if Input.is_action_pressed('up'):
		velocity = Vector2(speed, 0).rotated(rotation)

func _physics_process(delta):
func _process(delta):
	if Input.is_action_just_pressed("ui_shoot"):

func shoot():
	var bullet = bullet_path.instance()
	bullet.position = get_node("Position2D").get_global_position()
	bullet.velocity = get_global_mouse_position() - bullet.position

Edited to fix forum code formatting.

jgodfrey | 2023-05-04 19:56

To make sure your scene structure is clear, I assume it’d look like this (after shooting a few bullets). Is that correct?

   Player <-- posted script is here

Also, is your World scene the top-level node in the game?

jgodfrey | 2023-05-04 20:14

Further, I assume the problem has to be a difference in offsets between position and global_position here (though it’s not clear to me why that’s the case, which is why I asked about your scene structure).

bullet.position = get_node("Position2D").get_global_position()

Does this fix the problem?

bullet.global_position = $Position2D.global_position

(which is essentially a simplified version of your code, only assigning to bullet.global_position)

jgodfrey | 2023-05-04 20:22

i had this version in the beginning which is where i found the bug so i looked online and the closest solution was to switch it to what i have now

AirSpirit911 | 2023-05-04 20:32

Just add a “node” node to your scene and spawn your bullets there. This way it wont get affected by any parents. like

bullet.velocity = get_global_mouse_position() - bullet.global_position

horsecar123 | 2023-05-04 23:24