How to raycast with mouse

I want to create a raycast from a mouse cursor. Note that the player will be in first person. This is so I can create a terrain moddification system! Kind of like an FPS game.

So basically, I need to know how to create raycasts from a mouse in first person.
Thank you!

Check out ViewportPointToRay (or ScreenPointToRay depending on your UI inset usage)
This returns a unit ray in world space from where you want on the players’ screen, the example code shows the center of the viewport, but you can alter this to the position where they click at from the InputObject.Position from the click event.
You can turn this into a workspace raycast by referencing the origin and direction of the unit ray

3 Likes

get the players camera
fire the ray from “camera.CFrame.p”
fire the ray in “-camera.CFrame.LookVector*(numberofstuds you want it to travel)” direction

Use mouse.UnitRay to get the origin and direction of the mouse ray. Multiply the direction vector by whatever range you want because it’s a unit vector.

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