I am trying to create a projectile launcher, but when I launch my projectiles it launches 'from' the player, instead of the position it was already in.
The mechanic is this, I click an hold on the player object, drag the projectile and when I release the mouse button it should launch in the direction of the 'aim_line'
here is a video of the issue.
and here is my code:
extends KinematicBody2D
var mouse_over: bool = false
var is_selected : bool = false
var aim_dir: Vector2 = Vector2.ZERO
onready var aim_line: Line2D = $AimLine
onready var center_point: Position2D = $CenterPoint
var bomb_inst : RigidBody2D = null
export (PackedScene) var Bomb
func _ready():
print_var_change_event("aim Point [0]", aim_line.points[0])
print_var_change_event("player position",position)
func _on_Player_mouse_entered():
mouse_over = true
print("Mouse Over is true:" + str(mouse_over))
func _on_Player_mouse_exited():
mouse_over = false
print("Mouse Over is true:" + str(mouse_over))
func _physics_process(delta):
if (mouse_over): #I specifically want to check if the MOUSE_OVER variable is true before checking to see if the mouse button has been clicked, so I have done a layered if instead of AND
if Input.is_action_just_pressed("mouse_select"):
aim_line.add_point(get_global_mouse_position())
is_selected = true
bomb_inst = create_bomb_instance()
if (is_selected):
if (aim_line.points[1]):
aim_line.points[1] = get_local_mouse_position()
# print_var_change_event("aim point [1]", aim_line.points[1])
bomb_inst.global_position = get_global_mouse_position()
# print(bomb_inst.global_position)
if (!mouse_over && Input.is_action_just_released("mouse_select")):
if (aim_line.get_point_count() > 1):
bomb_inst.global_position = get_global_mouse_position()
aim_dir = get_aim_direction()
print_var_change_event("angle of aim",aim_dir.normalized())
print(get_global_mouse_position())
launch_projectile(bomb_inst, aim_dir)
aim_line.remove_point(1)
is_selected = false
func print_var_change_event(varName, var_value):
print(str(varName) + " changed to: " + str(var_value))
func create_bomb_instance():
var bomb_instance : RigidBody2D = Bomb.instance()
add_child(bomb_instance)
return bomb_instance
func launch_projectile(projectile: RigidBody2D, direction: Vector2):
projectile.apply_central_impulse(direction.normalized() * 2000)
func get_aim_direction():
return aim_line.points[0] - aim_line.points[1]