Raytracing (How do I return all parts found within my camera view)?

This is my current code

local camera = game.Workspace.CurrentCamera
local myCharacter = game.Players.LocalPlayer.Character
if not myCharacter or not myCharacter.Parent then
	character = game.Players.LocalPlayer.CharacterAdded:wait()
end
local direction = (game.Players.LocalPlayer.Character:WaitForChild("Head").Position - camera.CFrame.Position).Unit * 500
print("Test")
game:GetService("RunService").RenderStepped:Connect(function()
	local ray = Ray.new(
		camera.CFrame.Position,
		direction
	)
	
	local ignore = game.Players.LocalPlayer.Character 
	local hit, position = workspace:FindPartOnRay(ray, ignore)

	if hit and hit:IsA("BasePart") then
		hit.Transparency = 1 
		
	end	
end)

I tried using Camera.Focus but it gives me a render error. So I’m not sure how to go about this?

Oh this can be very computationally intensive. Typically when someone does this, they have an idea of which parts they actually want to find.

I imagine you wouldn’t want trees or random rocks in your result - what parts/objects do you want to find in the result? Knowing this, the computation can be simpler and more possible.

Yea I know the parts I would ideally filter out. Lets just say for example sake. It’s Part1, Part2, and Part3 inside the workspace. How would I go about filtering them if the camera focus is on either? Thanks.

Edit: woah just saw your name, man I remember you from way back in the day on the old school forums :smiley: glad to see you’re still around.

You can use Roblox’s internal function to translate their coordinates to screen views.

Camera:WorldToViewportPoint (roblox.com)

The second return of the tuple will be a bool of whether the part is on screen. This is relatively cheap to do for a set of parts in iteration.

--@localscript
local parts = {
	part1;
	part2;
	part3;
}

local cam = workspace.CurrentCamera

for _, part in pairs(parts) do
	if (select(2, cam:WorldToViewportPoint(part.Position))) then
		print("Part is in the camera view")
	end
end

I recognize your name from way back when too, great to see you still around. Many of the more veteran users seem to lurk off and on every now and then.

1 Like

Excellent, thank you. And yea, I quit around 2013, still kept small tabs on the game over the years. But I can’t believe the amount of growth the platform has had since it started. The top game used to be 800 people and now it’s 1.5 million on some days. It’s insane.

The studio has improved an incredibly amount too, and it’s so streamlined and easy and a very dedicated support team via the forums here. When I quit ROBLOX I got into warcraft3 modding making custom maps there. But the community and game is dead there, no real future. I think I’m going to be making a comeback to ROBLOX and start making maps again. It’s just so sophisticated and established compared to other game creator engines I’ve used.

1 Like