0 votes

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.

in Engine by (35 points)

1 Answer

+2 votes
Best answer

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
by (1,022 points)
selected by

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

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)

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.