+1 vote

I have a function that spawns a bullet, but somehow the start position of the bullet is wrong, please see the screenshot below:

enter image description here

Here's my current code:
For the enemy for spawning the bullet

func _spawn_bullet():
var bullet = bulletScene.instance()
bullet.start($BulletSpawn.global_position, currentAngle, damage)

and here's for the bullet:

extends KinematicBody2D
# properties
var bulletDamage = 0;
var targetPos = Vector2.ZERO
var velocity = Vector2()
var bulletAngle: float
var startPos
var bulletSpeed: int = 100
func start(origin: Vector2, angle: float, damage: int):
    startPos = origin
    bulletDamage = damage
    bulletAngle = angle
    velocity = Vector2(bulletSpeed, 0).rotated(bulletAngle)
func _ready():
    self.position = startPos
func _physics_process(delta):
    var collision = move_and_collide(velocity * delta)
    if collision:
        velocity = velocity.bounce(collision.normal)
        if collision.collider.has_method("hit"):
func _on_VisibilityNotifier2D_screen_exited():
Godot version 3.3.3
in Engine by (15 points)

Try setting the global_position you pass in the bullet's global_position by changing:

startPos = origin


global_position = origin

thank you very much @.@ you're a life saver

1 Answer

+1 vote

When you set the position of the bullet in the ready function, you set the local position of it in relation to the parent (owner).
But you get the global position of the Bulletspawner, so there might be some offset due to the difference

Just changing "self.position" to "self.global_position" in the bullets ready function should help

by (56 points)

yep, I'll take note of this. Thanks

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.