0 votes

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 {
MOVE,
ATTACK
}
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():
animationTree.active = true
func _process(delta):
match state:
MOVE:
move
state(delta)

    ATTACK:
        attack_state(delta)

func movestate(delta):
var input
vector = Vector2.ZERO
inputvector.x = Input.getactionstrength("moveright") - Input.getactionstrength("moveleft")
if Input.is
actionjustpressed("moveleft"):
$anchor.rotation
degrees = 180
if Input.isactionjustpressed("moveright"):
$anchor.rotationdegrees = 0
input
vector.y = Input.getactionstrength("moveback") - Input.getactionstrength("moveforward")
if Input.isactionjustpressed("moveback"):
$anchor.rotationdegrees = 90
if Input.is
actionjustpressed("moveforward"):
$anchor.rotation
degrees = 270
inputvector = inputvector.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)
    animationState.travel("Walk")
    velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
    animationState.travel("Idle")
    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
    get_parent().add_child(acid)

func attackstate(delta):
velocity = Vector2.ZERO
animationState.travel("Attack")

func attackanimationfinished():
state = MOVE

** Here is the bullet code:

extends Area2D

var speed= 200

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

func onVisibilityNotifier2Dscreenexited():
queue_free()

Godot version GLES3
in Engine by (16 points)
reshown by

1 Answer

0 votes

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

by (2,001 points)

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

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 Frequently asked questions and 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 [email protected] with your username.