I do not understand the difference between these, can someone tell me about the difference?
Could not find any topics comparing these, or any proper explanation on :ScreenPointToRay()
I do not understand the difference between these, can someone tell me about the difference?
Could not find any topics comparing these, or any proper explanation on :ScreenPointToRay()
I’d say that the documentation gives a fine overview of the difference. Camera | Documentation - Roblox Creator Hub
:ScreenPointToRay:
This function creates a unit Ray from a 2D position on the screen (defined in pixels), accounting for the GUI inset. The Ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera.
As this function acknowledges the GUI inset, the offset applied to GUI elements (such as from the top bar) is accounted for. This means the screen position specified will start in the top left corner below the top bar. For an otherwise identical function that does not account for the GUI offset, use Camera:ViewportPointToRay().
:ViewportPointToRay():
This function creates a unit Ray from a 2D position on the viewport (defined in pixels). This position does not account for the GUI inset. The Ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera.
As this function does not acknowledge the GUI inset, the viewport position given is not equivalent to the screen position used by GUI elements. If you are not using ScreenGui.IgnoreGuiInset and need an otherwise identical function that accounts for the GUI offset, use Camera:ScreenPointToRay().
So, does it mean ScreenPointToRay() uses Offset and ViewportFrame uses scale (between 0 and 1) ?
Both of them use pixel coordinates as arguments. Maybe you’ve noticed, that if you make a new ScreenGui and add a Frame with size ({1, 0}, {1, 0}), then at the top there will be a bit of space, that isn’t covered by the frame. That’s GuiInset. There is a property in ScreenGui called IgnoreGuiInset
, that would make the frame actually fill the entire screen. As the docs mentioned, with :ScreenPointToRay()
, the coordinates are taken from the top-left corner of the screen below the inset, whereas with :ViewportPointToRay()
, it’s taken from the very top left. As far as I have used it, it seems like you typically want to use :ScreenPointToRay()
when working with the player’s camera, and :ViewportPointToRay
when, for example, working with ViewportFrame
s
Thank you! I understand it now.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.