I Shoot for four directions

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

Hello, I’m still new to the godot engine, so I came across a following paradigm in which I’m stuck, does anyone have the solution for this code other than redoing the entire code?: <
Because I’m trying to structure a game based on my advance in the programming itself, and the command is very flawed and perhaps gigantic, but what it basically does is to rotate Position2D’s position so there is an instance with the bullet fired, At first it was all right when I triggered with if and elif conditions, but when I took out the elif started firing on the diagonals as well.
And the disadvantages of using the elif is that the character begins to no longer walk the diagonals.
Here the code:
Ps: Sorry I’m still developing my English
Direx =Direction x Direy = Direction y

if key_right:
	$ShotLocation.position = Vector2(40,0)
	direx = 1
elif key_left:
	$ShotLocation.position = Vector2(-40,0)
	direx = -1
else:
	$ShotLocation.position.x = 0
	direx = 0
if key_down:
	$ShotLocation.position = Vector2(0,40)
	direy = 1
elif key_up:
	$ShotLocation.position = Vector2(0,-40)
	direy = -1
else:
	$ShotLocation.position.y = 0
	direy = 0

My objective is: shot just four directions (w,a,s,d)

Amigoimaginario | 2019-07-28 17:39

Is there a question?

Eric Ellingson | 2019-07-28 18:02

Yes, I’m sorry if you got confused, but what I wanted to know is if I can use some calculation to make the vector2 not add to the other so there are not 8 possible shooting positions

Amigoimaginario | 2019-07-28 19:47

Do you mean like this?

if key_right:
    $ShotLocation.position = Vector2(40,0)
    direx = 1
elif key_left:
    $ShotLocation.position = Vector2(-40,0)
    direx = -1
elif key_down:
    $ShotLocation.position = Vector2(0,40)
    direy = 1
elif key_up:
    $ShotLocation.position = Vector2(0,-40)
    direy = -1

if !key_up and !key_down:
    $ShotLocation.position.y = 0
    direy = 0

if !key_right and !key_left:
    $ShotLocation.position.x = 0
    direx = 0

So the inputs don’t overlap?

Magso | 2019-07-29 15:50

:bust_in_silhouette: Reply From: greencloversguy

You could add all of the vectors together to create a result vector using if statements, no elif or else.

You could then use the angle method to check this result vector and see which way it is pointing or you could take the simpler approach and get the x and y atributes of the vector and check them.

if result.y == 0:
    if result.x == 1:
        $ShotLocation.position = Vector2(40, 0)
        direx = 1

etc…

The benefit of calculating a resulting vector is that you can handle situations such as if the player has the left and right keys down. In addition, you usually use this resultant vector to help you save time. In this example, I could set direx or direy to be result.x or result.y, respectfully!

Thank you, hadn’t thought of that :3

Amigoimaginario | 2019-08-04 19:09