0 votes

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 )
Godot version 3.0.0
in Engine by (22 points)

3 Answers

+1 vote
Best answer

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.

by (2,478 points)
selected by

It worked. thank you

+1 vote

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

 set_position().

From https://docs.godotengine.org/ru/stable/classes/class_node2d.html

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)

by (846 points)
+1 vote

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);
by (32 points)
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 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 webmaster@godotengine.org with your username.