This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello. I started learning game programming from yesterday. By following couples of youtube tutorials videos, I was able to make topdown player movement and shoot the bullet. However, when I put the bullet spawner as a instance child of the player in order to make it looks like the player is shooting the bulet, this wired thing happened. It seems like the bullet position is affected by the player rotation. How can I fix this?

enter image description here

The code for the Spawner:

extends Node2D

export var bulletScene : PackedScene

func _ready():
    pass # Replace with function body.

func _unhandled_input(event):
    if(event.is_action_pressed("fire")):
        var bullet = bulletScene.instance() as Node2D
        get_parent().add_child(bullet)
        bullet.global_position = self.global_position
        bullet.direction = (get_global_mouse_position() - global_position).normalized()
        bullet.rotation = bullet.direction.angle()

The code for the bullet:

extends KinematicBody2D

const speed = 290

export var smokeScene : PackedScene
export var bulletImpact : PackedScene

var direction = Vector2.ZERO

func _ready():
    pass # Replace with function body.

func _process(delta):
    var collisionResult = move_and_collide(direction * speed * delta)
    if collisionResult != null:
        var smoke = smokeScene.instance() as Particles2D
        get_parent().add_child(smoke)
        smoke.global_position = collisionResult.position
        smoke.rotation = collisionResult.normal.angle()

        var impact = bulletImpact.instance() as Node2D
        get_parent().add_child(impact)
        impact.global_position = collisionResult.position
        impact.rotation = collisionResult.normal.angle()
        queue_free()
Godot version ver3.3.2
in Engine by (82 points)

1 Answer

0 votes

instance the bullet outside the player scene use get_node("level").add_child(bullet) or bullet.set_as_toplevel(true) so the bullet doesn't inherit the parent's transform

by (454 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.