![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | amaou310 |
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?
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()