I’m trying to code a portal system for a game I’m working on, and I want to make sure there’s something behind the portal so you can’t place it on a surface too small.
The problem I’ve been encountering is that my raycasts aren’t interacting with the parts in the map, but they seem to have the correct start and end positions in relation to their normals.
I’ve already tried extending the raycast further by multiplying the normal by 10, and I can visually see the difference but the result is still the same. It seems to work consistently on certain faces of a block, but on others it simply wont place at all.
Before placing the portal I get the PortalSize through a model in replicated storage, and get the new PortalCFrame from the raycast hit position and the normal:
local PortalSize = ReplicatedStorage.Portal:GetExtentsSize()
local PortalCFrame = CFrame.lookAt(HitPosition, HitPosition-Normal)*CFrame.new(0, 0, PortalSize.Z/2)
if checkPortalBack(PortalCFrame, Normal, PortalSize) then
end
This is the function that actually checks whats behind the portal:
function checkPortalBack(PortalCFrame, Normal, PortalSize)
--Checks if something is behind portal
local Multipliers = {
{["X"] = 0.5, ["Y"] = 0},
{["X"] = 0, ["Y"] = 0.5},
{["X"] = 0.5, ["Y"] = 0.5},
{["X"] = -0.5, ["Y"] = 0},
{["X"] = 0, ["Y"] = -0.5},
{["X"] = -0.5, ["Y"] = -0.5},
{["X"] = 0.5, ["Y"] = -0.5},
{["X"] = -0.5, ["Y"] = 0.5}
}
local Vaild = true
for i,Multiplier in ipairs(Multipliers) do
local StartPosition = (PortalCFrame * CFrame.new(PortalSize.X*Multiplier.X, PortalSize.Y*Multiplier.Y, 0)).Position
local Raycast = workspace:Raycast(StartPosition, StartPosition-(Normal*10))
spawnBall(StartPosition, Color3.fromRGB(0, 255, 60))
spawnBall(StartPosition-(Normal*10), Color3.fromRGB(255, 234, 1))
if Raycast == nil then print("miss") Vaild = false else print("hit") end
end
return Vaild
end
The multipliers is an array of numbers that lets me get each corner and edge of the portal with 8 points, I loop through the array and then multiply the original PortalCFrame by the X and Y component, which should make it dependent on the cframe. I then spawn a part at the start and end position of each raycast, so I can visually see where the raycast starts and ends, the start is colored with green and the end with yellow. If every raycast hits something, the Vaild variable won’t be false and the portal will spawn.
This is what I see when I shoot the portal gun at this surface, the raycast start positions and end positions seem correct and they all hit, so the portal places
This is what I see when I shoot at the opposite face.
It looks like all the start and end positions are in the right place, but something somehow misses. It also should work on any angled surface for the most part, but it only works on some.