RTS selection box bug

This selection box change:

UIS.InputChanged:Connect(function()
	if ClickedOn then
		local X = Mouse.X - ClickedOn.X
		local Y = Mouse.Y - ClickedOn.Y

		if X < 0 then
			SelectionBox.Position = UDim2.fromOffset(ClickedOn.X + X, ClickedOn.Y)
			X = math.abs(X)
		end

		if Y < 0 then
			SelectionBox.Position = UDim2.fromOffset(ClickedOn.X, ClickedOn.Y + Y)
			Y = math.abs(X)
		end

		SelectionBox.Size = UDim2.fromOffset(X, Y)
		SelectionBox.Visible = true
	end
end)

Bugs on the right side of the screen. For somereason it scales up when you move and isnt like the left side which is the desired behavior. The desired behavior is like in other rts games such as a normal rts or tc3. Where it scales to your mouse

I’m not too familiar with this kind of thing, but in your first section of code you are just dealing with the X position but in the second section of code you have if Y < 0, then you do Y = math.abs(X)

1 Like

It already does that:


		if Y < 0 then
			SelectionBox.Position = UDim2.fromOffset(ClickedOn.X, ClickedOn.Y + Y)
			Y = math.abs(X)
		end

Is this a typo? Looks like Y = math.abs(X) should be Y = math.abs(Y)

1 Like

It was but even after i fixed it, the bug is still apparent.

robloxapp-20230613-1122258.wmv (661.6 KB)

Try this:

UIS.InputChanged:Connect(function()
	if ClickedOn then
		local MousePosition = Vector2.new(Mouse.X, Mouse.Y)
		
		local center = (ClickedOn + MousePosition) / 2
		local size = ClickedOn:Max(MousePosition) - MousePosition:Min(ClickedOn)
		
		SelectionBox.Position = UDim2.fromOffset(center.X, center.Y) -- Assumes SelectionBox has AnchorPoint (0.5, 0.5)
		SelectionBox.Size = UDim2.fromOffset(size.X, size.Y)
		SelectionBox.Visible = true
	end
end)

What did you change in code to make it work?

Basically I’m looking for the largest X and Y combination and subracting the smallest X and Y combination, I think that was the issue with your code, although I never tested anything.

Is it because of how my code handled negatives vs positives?

Your code didn’t take into account that ClickedOn could be less than Mouse position.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.