+1 vote

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))
Godot version Godot 3.3.2
in Engine by (13 points)

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.

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)

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

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.

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.