0 votes

Hi,
I am trying to create simple top down shooter. Player may look at any direction, and when we shoot, then I want to create new bullet instance and immediately start moving it to the mouse position (position it had when we shot).

I ended up with something like:

func _input(event):
    if(event.is_action_pressed("shoot")):
        var target = get_parent().get_global_mouse_pos()
        var start_pos = get_node("bulletSource").get_global_pos()
        var bullet = bulletScene.instance()
        bullet.set_pos(start_pos)
        bullet.init(target)
        world.add_child(bullet)
        #what next? what to do in bullet code

But I don't know how should I proceed with bullet moving.
Could you help me guys? Thanks in advance :)

in Engine by (56 points)

1 Answer

+1 vote

You need a direction vector for your bullet. A direction vector pointing from point A towards point B can be found via (B - A).normalized(). So in your case you can do this:

bullet.target_direction = (start_pos - target).normalized()

I don't know what your bullet code looks like, but for example if it's a KinematicBody2D, now you can do this:

move(target_direction * speed * delta)

Or if you're using an Area2D:

set_pos(get_pos() + target_direction * speed * delta)
by (21,920 points)

would you be able to elaborate on the

bullet.target_direction = etc

would you be able to explain what is happening there?

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.