How to make 8-way shooting in 2d game without raycast?

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

I am making a 2d game and i need to add shooting. I am begginer and i need help. Pls.

Why the need to do it “without raycast”?

jgodfrey | 2020-04-24 20:29

1 Like
:bust_in_silhouette: Reply From: caprinae

A good starting point would be to give your player a variable that determines its facing direction. You can use Vector2 to do this:

var facing_direction: Vector2 = Vector2.ZERO

func _physics_process():
  # reset facing direction
  facing_direction = Vector2.ZERO

  # get facing direction from inputs
  if Input.is_action_pressed("move_left"):  facing_direction += Vector2.LEFT
  if Input.is_action_pressed("move_right"): facing_direction += Vector2.RIGHT
  if Input.is_action_pressed("move_up"):    facing_direction += Vector2.UP
  if Input.is_action_pressed("move_down" ): facing_direction += Vector2.DOWN

Then, when you press the shoot button, you can instantiate a projectile on the player and set its velocity to the player’s facing_direction multiplied by the projectile’s movement speed. If you do it this way, projectiles will only ever travel in 8 directions.