Hi, I'm a total noob and just starting out trying to put a small game together and 'learning (the hard way) by doing'. But alas I've hit a snag - the bullets fly down instead of outwards from the muzzle (they fire from the right place but the direction is wrong).

I have a player scene (consisting of a RigidBody2D with a Position2d as Muzzle) and a bullet scene. From the player controls it looks like this

Player script:

if Input.is_action_pressed("ui_fireone"):
			$Muzzle.shoot()

Muzzle script:

extends Position2D
const Bullet = preload("res://Bullet.tscn")
onready var sound_shoot = $Shoot

func shoot ():
	var bullet = Bullet.instance()
	bullet.global_position = global_position
	bullet.set_as_toplevel(true)
	add_child(bullet)
	sound_shoot.play()
	return true

Bullet script:

extends KinematicBody2D
export var speed = 500

func _physics_process(delta):
	var velocity = Vector2 (0, speed).rotated (global_rotation)
	global_position +=  velocity * delta

I'm guessing that 'speed' in the bullet script is what's wrong, but I'm without a clue to solve it... Any help appreciated - exact instructions even more so :)

Thanks!

Hi, I'm a total beginner and just starting out trying to put a small game together and 'learning (the hard way) by doing'. But alas I've hit a snag - the bullets fly down instead of outwards from the muzzle (they fire from the right place but the direction is wrong). The player is able to turn 360 degrees and bullets should fly out from muzzle.

I have a player scene (consisting of a RigidBody2D with a Position2d as Muzzle) and a Bullet scene. From the player controls it looks like this Player script:

if Input.is_action_pressed("ui_fireone"):
			$Muzzle.shoot()

Muzzle script:

extends Position2D
const Bullet = preload("res://Bullet.tscn")
onready var sound_shoot = $Shoot

func shoot ():
	var bullet = Bullet.instance()
	bullet.global_position = global_position
	bullet.set_as_toplevel(true)
	add_child(bullet)
	sound_shoot.play()
	return true

Bullet script:

extends KinematicBody2D
export var speed = 500

func _physics_process(delta):
	var velocity = Vector2 (0, speed).rotated (global_rotation)
	global_position +=  velocity * delta

Any help appreciated - detailed instructions even more so :) Thanks!

Are you rotating the bullets when they are created by the muzzle? If not, then that could be causing the issue. You probably need to add something like bullet.global_rotation = global_rotation to the muzzle script, so the bullets are rotated in the correct direction.

@TwistedTwigleg said: Are you rotating the bullets when they are created by the muzzle? If not, then that could be causing the issue. You probably need to add something like bullet.global_rotation = global_rotation to the muzzle script, so the bullets are rotated in the correct direction. Thanks for the input, adding bullet.global_rotation = global_rotation after line 7 in the muzzle script works! Thank you!

3 years later