How to Raycast
Don’t link me the roblox devhub article for this because the script contains a lot of functions and confusing variables I just want to know how to raycast a laser that goes from your camera to your mouse and I want to know how to exclude certin parts
1 Like
I know this is a big problem right now because the old raycast is deprecated, just search 2019 raycasting then look at how to use raycasting 2021
Ok so here is the basics:
local origin = Instance.Position
local direction = (origin - Instance.Position) -- //A maybe can't fully remember
local ray = workspace:RayCast(origin, direction)
This shoots a ray in the direction of the part when the event “EventOfYourChoice” fires:
local caster = script.Parent
-- SETTINGS
local range = 100
EventOfYourChoice:Connect(function()
print("Fired!")
-- Set an origin and directional vector
local rayOrigin = caster.Position
local rayDirection = script.Parent.CFrame.LookVector * range
-- Build a "RaycastParams" object
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {game.Workspace.BlacklistedModel}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Cast the ray
local raycastResult = workspace:Raycast(caster.Position,rayDirection, raycastParams)
-- Checks if it hit and what it hit
if raycastResult then
local hitPart = raycastResult.Instance
print ("Hit ".. hitPart.Name)
local distance = (raycastResult.Position - caster.Position).Magnitude
print ("Distance: "..distance)
else
print ("Missed!")
end
end)