How do i make a bullet move in the mouse direction?

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

I’m making a top down game. So, I’m trying to make my player shoot a bullet (area2D) based on mouse position compared.
When I instace the bullet, I send the angle between the player and the global mouse position to the bullet. The bullet.gd looks like this:

func _process(delta):
#move_local_x(1)
position += (Vector2(cos(angle),sin(angle)).normalized())

But it goes wrong, when the bullet is instanced it mostly just goes at angle 0 or something close to that.
If someone could help me, I wold be very grateful.

:bust_in_silhouette: Reply From: Bartosz

I would advice to use normalized direction instead of angle e.g.

# in script where you generate bullet
var direction = (get_global_mouse_position() - player.global_position).normalized()
# pass direction to bullet


# inside bullet script
export(float) var bullet_speed = 0.5

func _process(delta):
    position += bullet_speed*delta*direction

Thank you very much! It works.
But do you know how to explain why the way that I did before didn’t worked?

Jutoend | 2018-03-24 23:09

I can only guess that code you used to calculate angle wasn’t doing what you thought it should(there are 3 different method on Vector2 to calculate angle)

Bartosz | 2018-03-24 23:17