Any way to detect if a part is hit by a ray

Is there a way to detect if a part is hit by a raycast?

1 Like

you can do

local result: RaycastResult = workspace:Raycast(origin: vector3,direction: vector3) result.Instance

1 Like

no the other way around

i mean like detecting if a part is hit by a raycast not if a raycast hits a part

1 Like

umm i don’t think there is a way to do that. the only way i can think of is create your own raycast logic and use it every single part of your script that use raycast like setting attribute when a raycast it a part

2 Likes

You can detect if the part intersects with a raycast by using a whitelist with just the target part. If nothing returns as a result, the part is not being hit by the ray. Here’s what the code would look like:

-- create raycast parameters, note these can be reused if looped for the same part
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params:AddToFilter(part)

local result = workspace:Raycast(origin,direction,params)
local intersects = result ~= nil -- true if the raycast intersects the part, false if not

Hope this helps!

1 Like

you would need to save what parts the ray hits by putting them into a table or changing an attribute on the part to true which then can be checked by other scripts

1 Like