What is ViewportPointToRay

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I wanna know what is ViewportPointToRay

  2. What is the issue? No issues

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yep but couldn’t underst

3 Likes

Well dev hub didn’t help me much

1 Like

I’d like to hear from the Devs

1 Like

ViewportPointToRay generates a ray from the origin, camera position and a direction facing where the camera is facing with a length of one. As stated from the devhub

The origin of the ray starts from your camera position and can be offset by the pixels of the viewport’s(or screen)'s X and Y given in the parameters.

Example of this is if you made the x value 0 then the origin of the ray will be the 3d position of the X location on your camera relative to the world, while if you called the function with camera.ViewportSize.X, being the maximum on the viewport in the 1st parameter then the ray’s origin will noticeably be moved to the right since 0,0 is top left of the screen

The direction is pretty simple to understand as it just points to where the camera is facing

The depth parameter is just an offset for how many studs forward you want the origin to be, where the default is 0

5 Likes

“The origin of the ray starts from your camera position and can be offset by the pixels of the viewport’s(or screen)'s X and Y given in the parameters.”

I thought the origin of the ray was at the tool’s Handle?

I think I’m just stupid but I have no idea what ViewportPointToRay is doing.

it’s 3 dimensional so it has x, y, z but it is NOT the origin of the ray in the 3d world (that’s the handle’s position)

so it has coordinates of 3 dimensions but which 3 dimensional metaverse is it referring to?

edit: yes I can see the values change as I zoom in and out, I just can’t figure it out

1 Like

Camera:ViewportPointToRay This explains what ViewportPointToRay is.

1 Like

sorry I should have said that I have gone over the documentation already along with the raycasting tutorial in the developer hub, but I still have no clue which metaverse these 3d coords are referring to

" The Ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera "

why can’t I picture this ray, I have never read a more confusing explanation of something in my life.

it’s the 3d position of the camera pointing towards the 2D replication of something else?

1 Like

ok so I’m back! Glad I got so frustrated attempting to learn this because now I have an understanding of what is going on, and WHY.

I only used the Laser gun casting ray developer pages to play around with how things worked I did not do any bullet shooting, only laser ray casting.

ViewportPointToRay is exactly what it says it is, sort of. It is shooting a laser from your eye balls (yes you sitting in the chair staring at the monitor) out to your mouse location.

*it has nothing to do with the gun tool location position at all, nothing.

Now do not count the distance your eye balls are from your monitor, negate that totally and stick your eye balls into your monitor and the laser is going from your eyes (Viewport) like Cyclops in the X-Men directly towards your mouse in the 3D world

Ok now we have that, lets figure out why it needs to do this.

In the end we need a ray coming out of our tool gun going out in the world towards our mouse position, this is the end goal.

But we do not know our MousePosition in the world so before we do anything with the gun we need to figure out our MousePosition in the 3D world.

This equation here is how everything is done.

FinalPosition = OriginPosition + DirectionVector

using this equation our MousePosition is the FinalPosition of the laser, that makes sense. So if we can figure out the other 2 variables then we are on our way.

Lucky that is exactly what ViewportPointToRay does.

ViewportPointToRay - the OriginPosition is where the camera or your eye balls in this case, are located in the world. So if you are zoomed out looking down on your character, picture 2 little eye balls way in the sky far back from your character, that is the OriginPosition.

ViewportPointToRay gives us the DirectionVector also which is the vector going from our eye balls out towards our mouse location in the 3d world.

so now we can calculate our MousePosition in the 3d world using our equation.

MousePosition = OriginPosition + DirectionVector

Ok awesome so we have our MousePosition in the 3D world, lets use it in combination with something else we know… we know our GUN TOOL location too!

Back to our equation

MousePosition = GunPosition + LaserVector

Obviously you see I substituted GunPosition for OriginPosition because you guessed it , that is the origin of where the laser should be coming out (not our eyes which are far back zoomed out on our character)

GunPosition is usually something like tool.Handle.Position

And I substituted LaserVector for DirectionVector because that is what it is in the world when we are doing ray casting with a laser.

so we have 2 of these variables known, MousePosition and GunPosition.

Lets solve for the unknown, the LaserVector, which is the actual laser that we want to be shooting.

LaserVector = MousePosition - GunPosition

the rest of it just has to do with seeing what intersects with our LaserVector to see what it will “hit”

4 Likes

Thx for your explaining but why can’t we just use Mouse.Hit.Position?

You can use Mouse.Hit.Position - workspace.CurrentCamera.CFrame.Position to get the same ray, but there are a few cases where you might want to use ViewportPointToRay for something other than the mouse position.

ViewportPointToRay is a method that is used to create a Ray from a 2D point on the player’s screen in the 3D game world.

When you click or tap on the screen in a Roblox game, the 2D position of your mouse or finger is converted into a 3D Ray that extends into the game world. This Ray can be used to determine what objects or surfaces the player is interacting with, such as shooting a gun or selecting an item.

The ViewportPointToRay method takes two arguments: a Vector2 that represents the 2D position on the player’s screen, and a float that represents the distance of the Ray from the camera. The method returns a Ray object that can be used to determine what objects or surfaces the player is interacting with.

local mouse = game.Players.LocalPlayer:GetMouse()
local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
local hitPart, hitPosition = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character)

In this example, mouse is a reference to the player’s mouse object, and ray is the Ray object that is created from the mouse’s position on the screen. The FindPartOnRay method is then used to determine what part the Ray intersects with in the game world, and the hitPart and hitPosition variables are set to the results of the method.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.