[Solved] How do I check if a ui is touching another ui

I am making space invaders for a project of mine.

I have made the movement and the enemy’s move. The only part I’m struggling with is the bullet.

I can make a bullet fire easily, but how would I detect if it’s touching another ui, the enemy.

I want it just like the classic spacer invaders, please if you know the solution tell me.

  • Thanks, MrOpScripter
7 Likes

There’s been a post similar to this before. Just searched it up on google.

1 Like

That’s so much!

I ended up with:

local function BoundaryCheck(Gui1, Gui2)
local pos1, size1 = Gui1.AbsolutePosition, Gui1.AbsoluteSize;
local pos2, size2 = Gui2.AbsolutePosition, Gui2.AbsoluteSize;

local top = pos2.Y-pos1.Y
local bottom = pos2.Y+size2.Y-(pos1.Y+size1.Y)
local left = pos2.X-pos1.X
local right = pos2.X+size2.X-(pos1.X+size1.X)
local In = true

if top > 0 then
	In = false
elseif bottom < 0 then
	In = false
end
if left > 0 then
	In = false
elseif right < 0 then
	In = false
end

return In

end

print(BoundaryCheck(script.Parent.test1, script.Parent.test2))

8 Likes

@RogueMage

Yeah, collisions don’t always work, do you know why?

I found out it’s partly because my script only counts it if it’s on top of it, if it’s touching the side of it it returns false. How can I fix this.

2 Likes

You might be able to fix that by adding to the size of one of the collision boxes by 1 in the equation. This would effectively make it detect other elements that are touching it’s exact border.

1 Like

@Protori
Well, that fixed the issue of it sometimes not hitting, but it’s destroying the one furthest to the left…

Also, now it’s X position has no effect on if it’s touching or not, it could be on the other side of the screen! All that matter is the Y position.

Code:
local function BoundaryCheck(Gui1, Gui2)
local pos1, size1 = Gui1.AbsolutePosition, Gui1.AbsoluteSize;
local pos2, size2 = Gui2.AbsolutePosition, Gui2.AbsoluteSize;

local top = pos2.Y-pos1.Y-(Gui1.Size.Y.Offset)
local bottom = pos2.Y+size2.Y-(pos1.Y+size1.Y)+(Gui1.Size.Y.Offset)
local left = pos2.X-pos1.X
local right = pos2.X+size2.X
local In = true

if top > 0 then
	In = false
elseif bottom < 0 then
	In = false
end
if left > 0 then
	In = false
elseif right < 0 then
	In = false
end

return In
end
1 Like

You could also use the rectangle formula: max( |x|/w, |y|/h ) <= 1

Here’s an example on Desmos: https://www.desmos.com/calculator/rqjjqmjp5f

Just check to see if any of the corners (x, y) satsify the formula.

1 Like

It didn´t work for me :confused:

I changed some parts in the code and managed to get down the amount of lines of code and fixed it so it worked for me. (Don’t know if I just used the code the wrong way)

				local pos1, size1 = Gui1.AbsolutePosition, Gui1.AbsoluteSize;
				local pos2, size2 = Gui2.AbsolutePosition, Gui2.AbsoluteSize;
				
				local top = (pos1.Y) - (pos2.Y)
				local bottom = (pos2.Y) - (pos1.Y)
				local left = (pos1.X) - (pos2.X)
				local right =(pos2.X) - (pos1.X)
				local touching = false

				if top < (size1.Y) and bottom < (size1.Y) and left < (size1.X) and right < (size1.X) then
					touching = true
				end

                return touching
1 Like
local function collidewith(gui1,gui2)
	local gui1_topLeft,gui1_bottomRight = gui1.AbsolutePosition, gui1.AbsolutePosition + gui1.AbsoluteSize
	local gui2_topLeft,gui2_bottomRight = gui2.AbsolutePosition, gui2.AbsolutePosition + gui2.AbsoluteSize
	return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y))
end
7 Likes