I can’t seem to figure out what is the problem here. Why the WorldToScreenPoint is giving me very small numbers when I am giving it a worldpoint in the middle of the screen so would expect it to give back a few hundred pixel values for X and Y on the centre of the screen.
local vector, onScreen = camera:WorldToScreenPoint(hitPosition)
local screenPointX = vector.X
local screenPointY = vector.Y
print("hitPosition: " .. tostring(hitPosition))
print("screenPointX: " .. tostring(screenPointX))
print("screenPointY: " .. tostring(screenPointY))
and the output is:
hitPosition: 58.0399475, 7.59386587, 472.34314 -- this is about on the centre of the screen where the player character hits it
screenPointX: 0.1736513376236
screenPointY: 0.0090127885341644
What am I doing wrong or not understanding here?
Please help!
Thank you @CoderHusk good suggestion.
I did not try to check that myself since I am sure the position I was feeding it is on screen as I am using that in other parts of the code.
But it is false all the time, no matter which way I am looking.
I also tried to feed it with a known world point like in the example in the developer documentation and it still is false:
local worldPoint = Vector3.new(0, 0, 0)
local vector, onScreen = camera:WorldToScreenPoint(worldPoint)
Hmm, I copied the script over to an empty project and there it says onScreen is true all the time and gives the same values all the time no matter which way I rotate the camera.
OK, I guess this might be obvious for most but wasn’t for me.
I was doing the whole thing in a serverscript.
Now after sending the world position over to a local script and doing the
camera:WorldToScreenPoint(worldPoint) there the values it gives are perfect for positioning the GUI object.