Interesting conundrum, on start when my camera is static my selection box is fine, but when my camera moves the selection box loses it's accuracy completely despite allegedly getting it's co-ordinates each click, why would this be? Here's what I've got so far.
extends Node2D
var isDragging = false
var selectedUnits = []
var dragStart = Vector2()
var selectedRectangle = RectangleShape2D.new()
func _physics_process(delta):
if Input.is_action_just_pressed("RightClick"):
var clickPosition = get_global_mouse_position()
if selectedUnits.size() > 0:
for selection in selectedUnits:
if selection.collider.is_in_group("Selectable"):
selection.collider.beanSoldierAgent.target_position = clickPosition
selection.collider.speed = 300
func _unhandled_input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.is_pressed():
isDragging = true
dragStart = event.position
for selection in selectedUnits:
if selection.collider.is_in_group("Selectable"):
selection.collider.DisableSelection()
selectedUnits = []
elif isDragging == true:
isDragging = false
queue_redraw()
var dragEnd = event.position
selectedRectangle.size = (dragEnd - dragStart).abs()
var space = get_world_2d().direct_space_state
var query = PhysicsShapeQueryParameters2D.new()
query.set_shape(selectedRectangle)
query.transform = Transform2D (0, (dragEnd + dragStart) / 2)
selectedUnits = space.intersect_shape(query)
for selection in selectedUnits:
var selectionCollision = selection.collider
if selectionCollision.is_in_group("Selectable"):
selectionCollision.EnableSelection()
print(selectedUnits.size())
if event is InputEventMouseMotion and isDragging == true:
queue_redraw()
func _draw():
if isDragging == true:
draw_rect(Rect2 (dragStart, get_global_mouse_position() - dragStart), Color.DARK_GREEN, false)
And just in case the camera might be causing issues.
extends Camera2D
func _process(delta):
if Input.is_action_pressed("W"):
position -= Vector2(0, 500) * delta
elif Input.is_action_pressed("S"):
position += Vector2(0, 500) * delta
if Input.is_action_pressed("A"):
position -= Vector2(500, 0) * delta
elif Input.is_action_pressed("D"):
position += Vector2(500, 0) * delta
All else I've done as well is enable camera smoothing.