+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?

Code:

func Shoot(delta):
    $Weapon.look_at(get_global_mouse_position())

if Input.is_action_pressed("click"):
    var bullet_instance = bullet.instance()
    bullet_instance.position = $Weapon.get_global_position()
    bullet_instance.rotation_degrees = rotation_degrees
    get_parent().add_child(bullet_instance)
    bullet_instance.apply_impulse(Vector2(), Vector2(bulletSpeed, 0).rotated(rotation))
    get_tree().get_root().add_child(bullet_instance)
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 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.