0 votes

My game uses only keyboard keys NO MOUSE, and I'm trying to get a projectile (Area 2d) to move in different directions based on the direction the player is facing (i.e. shot up, right, left, or down). Currently it only shoots to the right but does spawn from the correct directions based on the players facing direction, but the projectile always goes right.

Here is the players code for the projectile:
`if Input.isactionjustpressed("acid throw"):
state = ATTACK
var acid = ACID.instance()
if sign($anchor/Position2D.position.x) == 1:
acid.position = $anchor/Position2D.global
acid.position = $anchor/Position2D.globalposition

func attackstate(delta):
velocity = Vector2.ZERO

func attackanimationfinished():
state = MOVE`

Here is the projectiles code:
extends Area2D
var speed= 200
var velocity = Vector2()
var xdir = 0
var y
dir = 0
var direction = 1
func physicsprocess(delta):
velocity.x = xdir
velocity.y = y
position += transform.x * delta * speed
velocity.x = speed * delta * direction

func setaciddirection(dir):
direction = dir

func onVisibilityNotifier2Dscreenexited():

Godot version GLES3
in Engine by (16 points)

1 Answer

0 votes

your bullet moves only because this line

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

you can try remove other line, it still runs the same
The problem is it doesn't have any direction attached to it
you only increase its position every frame. that why it only moves right
the direction you have collected is in the variable 'velocity' but you didn't do anything with it after that
make your position move by your velocity instead.

func _physics_process(delta):
    velocity.x = speed * delta * direction
    position += velocity
by (133 points)

thanks for your help but it still only goes right I'm afraid

try print dir value in setaciddirection(dir) function to see if it got dir correctly
if you turn left it should be -1

its getting dir because its printing "left" everytime i fire but still goes the same way for some reason

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.