0 votes
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

in Engine by (21 points)
edited by

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

1 Answer

0 votes
Best answer

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
by (10,421 points)
selected by

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

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.

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.