Make bullet go in the direction the player is facing without using the mouse?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By djskidds

Im creating a 2d game using keyboard for all buttons. When i press the shoot button the bullet always goes to the right. I have been able to get it to spawn from other directions but it still faces and moves to the right. How do i get the bullet to rotate and move in the direction the player is facing (ie. facing up bullet goes up). Below is my player code. Bullet code is near the bottom. Thank you in advance.

extends KinematicBody2D

const ACCELERATION = 500
const MAX_SPEED = 80
const FRICTION = 500

enum {
var can_fire = true
var state = MOVE
var velocity = Vector2.ZERO
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get(“parameters/playback”)
const ACID = preload(“acid.tscn”)

func _ready(): = true
func _process(delta):
match state:


func move_state(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”)
if Input.is_action_just_pressed(“move_left”):
$anchor.rotation_degrees = 180
if Input.is_action_just_pressed(“move_right”):
$anchor.rotation_degrees = 0
input_vector.y = Input.get_action_strength(“move_back”) - Input.get_action_strength(“move_forward”)
if Input.is_action_just_pressed(“move_back”):
$anchor.rotation_degrees = 90
if Input.is_action_just_pressed(“move_forward”):
$anchor.rotation_degrees = 270
input_vector = input_vector.normalized()

if input_vector != Vector2.ZERO:
	animationTree.set("parameters/Idle/blend_position", input_vector)
	animationTree.set("parameters/Walk/blend_position", input_vector)
	animationTree.set("parameters/Attack/blend_position", input_vector)"Walk")
	velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
	velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)

velocity = move_and_slide(velocity)

if Input.is_action_just_pressed("acid throw"):
	state = ATTACK
	var acid = ACID.instance()
	acid.position = $anchor/Position2D.global_position
	acid.position = $anchor/Position2D.global_position

func attack_state(_delta):
velocity = Vector2.ZERO“Attack”)

func attack_animation_finished():
state = MOVE

** Here is the bullet code:

extends Area2D

var speed= 200

func _physics_process(delta):
position += transform.x * delta * speed

func _on_VisibilityNotifier2D_screen_exited():

:bust_in_silhouette: Reply From: Gluon

I would suggest that you add an X and Y variable in the bullet and then when you create a bullet you can set these to fire it left and right.

In your player have a variable (I will call it bullet_set) to record player direction

Var bullet_set = "right"

then when you input a movement to the player have an appropriate setting for that boolean e.g.

if Input.is_action_just_pressed("moveleft"):
    bullet_set = "left"

then have the following for the bullet

var X_dir = 0
var Y_dir = 0

func _process(delta):
    velocity.x = X_dir
    velocity.y = Y_dir
    move_and_slide(velocity.normalized() * SPEED * delta)

then finally when you create the bullet just set the bullet instances x and y dir based on the var bullet_set

while it seems helpful I cant get it to work seeing that I have the bullet being a Area2D and move and slide isn’t declared in the current class as it says is there anything other way I can get it to work without changing it because I like what I have so far

djskidds | 2022-04-08 17:28