Overlapping Transparency and Raycasting

I’m new to lua coding and working on a game with my kids. We are creating a tower-defense game and I could use some help with transparency. We are using semi-transparent block parts around the paths and towers to show the buffers where it is valid/invalid to place towers

My issue is when transparent objects overlap the transparencies they look darker in the combined area, which I understand is an expected behavior. I would like everything to look uniform.

I first set it up so when the path is drawn & towers are added/removed it would union the buffers together which gave it the uniform appearance I was looking for. Unfortunately, it also caused the raycasting to erroneously detect the edges of the buffers. Areas that should be valid placements outside the buffer appear to be valid placement, but the raycast shows it as a buffer part - I can see where those areas are when I turn on Show Decomposition Geometry and, after some research, I found that this is also common.

The raycast I’m using is the following. The only raycastParam is a blacklist for the temporary tower so it doesn’t block the ray:

local raycastResult = workspace:Raycast(mouseRay.Origin,mouseRay.Direction * 1000, raycastParams)

My next thought which I think might actually work would be to create a part that is a duplicate of the map ground. Then create a union of the path & tower buffers and and cut it out of a clone of the ground duplicate which would leave a negative image of the buffer borders. Then I can cut out the new part from another clone of the ground map which would leave the actual buffer borders and I could make that part transparent.

It just seems very cumbersome and I wanted to see if there is anything more elegant that might work? I appreciate your help!

1 Like

You want to be able to place it inside a transparent object?

1 Like

I’m creating transparent parts called “Buffer” that overlap the parts I don’t want them to place towers (on or just outside the path, a certain distance around other towers). In the simplest form, think of a “U” shaped path. The buffer part would be about 1 stud wider than the path on each side and 0.1 studs taller and be 1/2-transparent with an orange color. The raycast checks to see if the mouse is over the ground, so the buffer part would block that part and return the buffer rather than the ground. When I union the objects, Roblox adds in diagonal sections around the corners that aren’t visible but still cause the raycast to return the buffer rather than the ground.

I’m just trying to be able to show the transparent buffers without it making the overlapped areas darken (like around the corners of the path, or where the tower buffers overlap the path buffer or other tower buffers).

Not even sure my cumbersome solution would work, just trying to think of a way to do it.

Here’s the code for creating the union (runs whenever map loads and on each tower add/removal) and for showing the buffer.

Create unioned buffer:

function round.UnionBuffers()
	local newUnion = nil
	local partsList = {}
	local map = workspace.Map:FindFirstChildOfClass("Folder")
	
	if map:FindFirstChild("Buffers") then
		map.Buffers:Destroy()
	end
	
	for i,part in pairs(map.Path:GetChildren()) do
		table.insert(partsList,part.Buffer)
	end
	for i,part in pairs(workspace.Towers:GetChildren()) do
		table.insert(partsList,part.Buffer)
	end
	
	if #partsList > 1 then
		local firstPart = partsList[1]
		table.remove(partsList,1)
		newUnion = firstPart:UnionAsync(partsList)
	else
		newUnion = partsList[1]
	end	
	newUnion.Name = "Buffers"
	newUnion.Parent = map
end

Show buffer:

local map = workspace.Map:FindFirstChildOfClass("Folder")
		map.Buffers.Transparency = 0.5