WorldToScreenPoint is not a valid member of Vector2

I’m doing something that tells me if an object is in the middle of the screen but I get the title error

while wait() do
	local camera = workspace.CurrentCamera.ViewportSize / 2

	local worldPoint = workspace.detecciontest
---error here

	local vector, onScreen = camera:WorldToScreenPoint(worldPoint.Position)

-----------------------------------------------------------------------------
	local screenPoint = Vector2.new(vector.X, vector.Y)
	local depth = vector.Z
	
	print(onScreen)
end

how can i do it?

You are referring to ViewportSize property, dividing it by 2 and call it camera, you may want split it into two lines

workspace.CurrentCamera.ViewportSize *= .5
local camera = workspace.CurrentCamera

i put it here

workspace.CurrentCamera.ViewportSize *= .5
local camera = workspace.CurrentCamera
while wait() do
	local worldPoint = workspace.detecciontest

	local vector, onScreen = camera:WorldToScreenPoint(worldPoint.Position)

	local screenPoint = Vector2.new(vector.X, vector.Y)
	local depth = vector.Z
	
	print(onScreen)
end

but i got the error= can’t set value in the 1st line

What? Never hear of that kind of errors. Anyway, just make it like that:

local camera = workspace.CurrentCamera
camera.ViewportSize *= .5

while wait() do
	local worldPoint = workspace.detecciontest

	local vector, onScreen = camera:WorldToScreenPoint(worldPoint.Position)

	local screenPoint = Vector2.new(vector.X, vector.Y)
	local depth = vector.Z
	
	print(onScreen)
end

image

Oh, didnt checked that Camera | Roblox Creator Documentation is read only, not sure what you are doing this for, may you just remove the second line?

ViewportSize is a ready-only value, you may want to set camera.FieldOfView instead.

Or you could use the field of view to calculate this instead. You’d also want to use WorldRoot | Roblox Creator Documentation to determine if there are any part bounds extending from the position that are in the camera’s view.

yeah What I mean is that I want to do something similar to this but only detect if it is in the middle of the screen


sorry for the music i dont have obs

local camera = workspace.CurrentCamera

while wait() do
	local worldPoint = workspace.detecciontest

	local vector, depth= camera:WorldToScreenPoint(worldPoint.Position)

	local delta = Vector2.new(vector.X, vector.Y)-camera.ViewportSize/2
	

	print(delta.magnitude<camera.ViewportSize.X/5)
end
1 Like

thanks you bro, it works.Is there a way to make the detection center smaller?
im sorry I’m still learning