#2D camera boundaries
1 messages · Page 1 of 1 (latest)
Instead of 1 or 2 rays, cast several rays around the camera direction and take the closest hit.
Vector3.new(0, 0, 0),
Vector3.new(0.5, 0, 0),
Vector3.new(-0.5, 0, 0),
Vector3.new(0, 0.5, 0),
Vector3.new(0, -0.5, 0),
}
local closestDist = MAX_DIST
local finalPos = desiredPos
for _, offset in ipairs(offsets) do
local dir = (desiredPos + offset) - root.Position
local result = workspace:Raycast(root.Position, dir, params)
if result then
local dist = (result.Position - root.Position).Magnitude
if dist < closestDist then
closestDist = dist
finalPos = result.Position + result.Normal * 0.3
end
end
end
camera.CFrame = CFrame.new(finalPos, root.Position)```