Move object to some direct (for example - mouse pos)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By desik
:warning: Old Version Published before Godot 3 was released.

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 :slight_smile:

:bust_in_silhouette: Reply From: kidscancode

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)

would you be able to elaborate on the

bullet.target_direction = etc

would you be able to explain what is happening there?

kc | 2017-10-12 01:53