the bullet are not spawning in the right place

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

the bullet are not spawning in the position2D, instead are spawning bellow my character.

extends KinematicBody2D

const FIREBALL_SCENE = preload(“res://agora vai cenas/tiro.tscn”)

var speed = 100

func _process(delta):

var dir_x = 0
var dir_y = 0

if Input.is_action_pressed("ui_right"):
	dir_x += 1
if Input.is_action_pressed("ui_left"):
	dir_x -= 1
if Input.is_action_pressed("ui_up"):
	dir_y -= 1
if Input.is_action_pressed("ui_down"):
	dir_y += 1
	
elif Input. is_action_just_pressed("shoot"):
	var fireball = FIREBALL_SCENE.instance()
	get_parent().add_child(fireball)
	fireball.set_position(get_node("Position2D").get_global_position())
	
	
translate( Vector2(dir_x, dir_y ) *delta *speed )
:bust_in_silhouette: Reply From: BVictor

First check if the main position of bullet scene is 0. and later you need set the position
before adding to parent. like:

fireball.position = $Position2D.get_global_position();
get_parent().add_child(fireball);
:bust_in_silhouette: Reply From: Lopy

If get_parent() has a non-zero position, you will have offset issues. You could make the fireballs children of a Node Node, so that their position is always global.

You could try fireball.global_position = get_node("Position2D").get_global_position().

If it still does not work, and the bullets spawn with a constant offset, check that the Sprite etc all have a position of zero in their scene.

It worked. thank you

flinmer | 2021-02-09 00:19

:bust_in_silhouette: Reply From: yrtv

Fireball must be the child of Position2D if you want to use

 set_position().

From Node2D — Документация Godot Engine (4.x) на русском языке

Position, relative to the node’s parent.

But you pass to it global world position coordinates of Position2D with

("Position2D").get_global_position()

Make Fireball child of Position2D and spawn at (0, 0)