2D Shoot Mechanics: Top down, mouse aim?

Attention Topic was automatically imported from the old Question2Answer platform.

Hello! Im quite new to Godot engine, and even with a few tutorials and the manual, I still need help understanding what im doing wrong here…

In essence: I have a character with its own movement script. How can i shoot a bullet from my character towards my mouse position in a 2D game, while using an area2D node?

I’ve tried to implement the following code, but the trajectory of the projectile makes little sense to me…

`Gun Script`

``````extends Area2D
var shoot

export (PackedScene) var laser

func _shoot():
var pew = laser.instance()

pew._start_at(get_angle_to(get_global_mouse_position()), get_position())
``````

`Laser shot`

``````extends Area2D
var velocity = Vector2()
export var speed = 1000

func _start_at(dir, pos):
set_rotation(dir)
set_position(pos)

velocity = Vector2(speed, 0).rotated(dir + PI/2)

func _process(delta):
set_position(get_position() + velocity * delta)
``````

It just sorta…goes all over the place. I’ve seen a few instances of people using rigidbodies or kinematics to do it, but i wanted to keep the project simple, with the option of returning and altering it at a later time.

Ok fast course on shooting lasers in desire direction (my personal flavor)

Lets split shooting in steps
Inside gun script:

1. Instantiate laser `var pew = laser.instance()`
2. Make laser adjust itself to your design: `laser.shot(global_position,get_global_mouse_position())`

Inside laser script in `shoot` method:

1. set it in the right place `global_position = gun_position`
2. find in what direction laser is shoot `var direction = (aim_position - global_position).normalized()`
3. make laser point in that direction `rotation = direction.angle()`
4. make laser moving `rotation = direction.angle()`

Inside laser script in `_process` method:

1. make it fly in desired direction `position += direction * speed * delta

code would look like this:

Gun script:

``````extends Area2D
var shoot

export (PackedScene) var laser

func _shoot():
var pew = laser.instance()

pew.shoot(get_global_mouse_position(), global_position)

``````

Laser script

``````extends Area2D
var direction = Vector2()
export var speed = 1000

func shoot(aim_position, gun_position):
global_position = gun_position
direction = (aim_position - gun_position).normalized()
rotation = direction.angle()

func _process(delta):
position += direction * speed * delta
``````

Awesome Bartosz! Just one small detail: My laser shot ends up going sideways in the direction of the click (its kindda like a bolt or capsule instead of round bullet, so its sideways)…any tips?

Thanks again!

jim | 2018-03-25 03:56

If I understand you correctly you’re saying that lase sprite is rotated 90 degrees of what it suppose to be. To correct that just apply adjustment rotation. Because `90degrees == 0.5*PI radians` you need to modify this `rotation = direction.angle()` to that `rotation = direction.angle() + 0.5 *PI`

Bartosz | 2018-03-25 13:31

That did the trick! Thanks again Bartosz!

jim | 2018-03-26 05:00

Thanks, that helped me!

Goathier | 2020-02-04 03:59