Casting ray from my mouse?

How would i make a raycast from my mouse 20 studs down?

This is what i have:

local MouseRay = Ray.new(Item.PrimaryPart.Position, Need mouse here + Vector3.new(0,-20,0))

You can use UserInputService and Camera:ViewportPointToRay to create a Ray from your current mouse position, as so:

local pos = game:GetService("UserInputService"):GetMouseLocation()
local rayOrigin = workspace.CurrentCamera:ViewportPointToRay(pos.X, pox.Y)

You can now modify this Ray to make it go 20 studs down. Did you mean that the origin is 20 studs down or whatever the ray will hit?

Note that this code only works in a LocalScript as it needs a mouse

I meant that the part that the ray should hit is 20 studs down, Using vector3.new(0,-20,0)

So you can create a new ray whose origin is 20 studs lower. Using the code from beefore, you can do:

local offsetRay = Ray.new(rayOrigin.Origin - Vector3.new(0, 20, 0), rayOrigin.Direction)
1 Like