How do I make a projectile go to the position of my mouse

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

I am trying to create a projectile that is fired from the position of a child node of type sprite, the projectile is created and fired but always goes to the right, never towards the mouse position. What am I doing wrong?


func Shoot(delta):
if Input.is_action_pressed("click"):
	var bullet_instance = bullet.instance()
	bullet_instance.position = $Weapon.get_global_position()
	bullet_instance.rotation_degrees = rotation_degrees
	bullet_instance.apply_impulse(Vector2(), Vector2(bulletSpeed, 0).rotated(rotation))

Have you looked into using a bit of code like this: get_viewport().get_mouse_position()? If you’re wondering, I found the code here.

Ertain | 2021-06-03 00:35

I’d guess it has something to do with your bullet’s trajectory in your bullet instance - check that you’re not accidentally rotating the bullet in the wrong direction. This can easily happen if your gun sprite is “facing” left but the bullet trajectory is going right (position.x + n)

delphonso | 2021-06-03 12:06

In tutorials covering bullet shooting the bullet is only parented to the root therefore I would suggest removing this line: get_parent().add_child(bullet_instance). The movment of the parent afftects its children which could explain the weird bullet behaviour.

Axiot | 2021-06-04 01:55

Why do you add the bullet as a child of two separate nodes?

exuin | 2021-06-03 22:37