0 votes

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

in Engine by (24 points)

Why the need to do it "without raycast"?

1 Answer

0 votes

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.

by (266 points)
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 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 webmaster@godotengine.org with your username.