How to shoot projectiles in top down 2D without mouse.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By StaveYt
export (int) var speed=200
export (float) var rotation_speed=0.33333333;
export (float) var gun_cooldown
export (int) var health


var velocity = Vector2()
var rotation_direction=0
var can_shoot = true
var alive = true
var bullet = preload ("res://src/Scenes/Bullet.tscn")

func _physics_process(delta):
    get_input()
    Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
    rotation += rotation_direction * rotation_speed * delta
var direction = Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
Input.get_action_strength("move_down")*0.7 - Input.get_action_strength("move_up")*0.7
)
velocity = speed * direction
move_and_slide(velocity) 

func get_input():
rotation_direction=0
velocity=Vector2()
if Input.is_action_pressed("rot_right"):
	rotation_direction +=0.04
if Input.is_action_pressed("rot_left"):
	rotation_direction -=0.04

I making a top down arcade shooter and I didn’t want to use the mouse for controls so I was wondering was there a way i could make the projectiles fire in the direction I’m facing.
My sprite has an arrow on it to show the direction you’re facing (it is on the same sprite as the player).

If anyone knows please help me! Thanks!

PS: My scene for the player goes like this

Player

O (the sprite)
CollisonShape2D

Hi,
you best provide your player movement code and the sceneTree setup to get some proper help.

klaas | 2020-09-03 20:01

:bust_in_silhouette: Reply From: njamster

Add the following to the bottom of your get_input()-function:

if Input.is_action_pressed("shoot"):
    var new_bullet = bullet.instance()
    new_bullet.global_position = self.global_position
    new_bullet.direction = Vector2.RIGHT.rotated(rotation)
    add_child(new_bullet)

Make sure you’ve defined an InputAction called “shoot”! Pressing any key associated with it will spawn a new bullet-instance, place it where the player stands and then adjust it’s direction based on the players rotation. Your bullet-scene then should take this direction into account when actually moving, e.g.:

var direction = Vector2.RIGHT
var speed = 400 # pixels / s

func _ready():
    set_as_toplevel(true) # move independent from parent node

func _physics_process(delta):
	global_position += direction * speed * delta

It didn’t work. Well kinda, it creates the when i press space (the button for shoot) bullet but doesn’t fire it . Tell me if i did anything wrong. I just copied and pasted the code:

the top part i copied and pasted under get_input()-function

func get_input():
rotation_direction=0
velocity=Vector2()
if Input.is_action_pressed("rot_right"):
	rotation_direction +=0.04
if Input.is_action_pressed("rot_left"):
	rotation_direction -=0.04
if Input.is_action_pressed("shoot"):
	var new_bullet = bullet.instance()
	new_bullet.global_position = self.global_position
	new_bullet.direction = Vector2.RIGHT.rotated(rotation)
	add_child(new_bullet)

The bottom part into the script for the bullet (Nodes go like this

Bullet (RigidBody2D)

Bullet (the sprite)
CollisionShape2D

)

extends RigidBody2D
var direction = Vector2.RIGHT
var speed = 400 # pixels / s

func _ready():
    set_as_toplevel(true) # move independent from parent node

func _physics_process(delta):
    global_position += direction * speed * delta

StaveYt | 2020-09-05 15:35

Given the player and the bullet both have a CollisionShape2D to them, you might need to offset the bullets spawning position to avoid spawning the bullet inside the player.

njamster | 2020-09-05 16:06