Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Camera:WorldToScreenPoint doesn't work or works incorrectly?

I'm trying to play a sound when i look at a part. I read wikia and saw that some people recommended WorldToScreenPoint so i tried it out. As the title suggests my script didn't work and I have no idea why. It just played the sound at the start of the game and didn't work when looking at the part. I also tried RayCasting but I failed miserably. Any idea why it plays the sound at the start of the game?

Code:

local Camera = workspace.CurrentCamera
local lookpart = game.Workspace.lookpart

game.Players.PlayerAdded:Connect(function(plr)
local _, withinScreenBounds = Camera:WorldToScreenPoint(lookpart.Position)
if withinScreenBounds then
game.Workspace.Sound:Play()
	print("looked")
	end
end)

1 answer

✓ Accepted answer

WorldToScreenPoint does work and you are using it correctly but you seem to be expecting a different outcome.

WorldToScreenPoint's 2nd Variable returns true if the given position is in the screen.

The Script Below Should Always Print "Cannot See Point"

local cam = workspace.CurrentCamera

game:GetService("RunService").Stepped:Connect(function()
	local _, withinScreen = cam:WorldToScreenPoint(cam.CFrame.Position-cam.CFrame.LookVector*10)
	if withinScreen == false then
		print("Cannot See Point")
	elseif withinScreen == true then
		print("Can See Point")
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.