Godot Version
4.3 STABLE
Question
I’m trying to make a troop selection mechanism for my 2d rts. I have the following code so far, it works most of the time but sometimes it get’s jittery and acts weird, where the rect just freezes as it was and like doesn’t resize. Is there something wrong with the code or is it just my laptop?
extends Node2D
var selection_rect: Panel
var dragging = false
var init_pos = Vector2()
var curr_pos = Vector2()
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
if event.is_pressed():
print("PRESSED AT: ", event.position)
dragging = true
init_pos = get_global_mouse_position()
selection_rect = Panel.new()
add_child(selection_rect)
if event.is_released():
print("RELEASED AT: ", event.position)
dragging = false
selection_rect.queue_free()
if event is InputEventMouseMotion:
if dragging:
var curr_pos = get_global_mouse_position()
var begin = Vector2(minf(init_pos.x, curr_pos.x), minf(init_pos.y, curr_pos.y))
var end = Vector2(maxf(init_pos.x, curr_pos.x), maxf(init_pos.y, curr_pos.y))
selection_rect.set_begin(begin)
selection_rect.set_end(end)