Raycast toward a part's position?

Greetings, devforumers!

I’ve been trying to create a vehicle damage system with raycasting instead of the .Touched event (because it can do some serious lag with a high number of parts, and from now on i will only use the event for hitboxes.)

Everything else is working as intended, but the problem is that i can’t get the ray to go directly to the part that was hit.

Here is a snippet of the code that does the raycast:

local AreaRay = Ray.new(Hitbox.Position, Part.Position * 8)
local Whitelist = {Part}
local Hit, Pos, Normal = workspace:FindPartOnRayWithWhitelist(AreaRay, Whitelist, true)

I guess i might just be doing something wrong, but i really don’t know.

The second argument of the Ray.new constructor is a direction argument. By providing the world coordinates of the parts position your ray is pointing in the direction of the part from the world origin, not the part from your hitbox position. These two are not the same unless your hitbox is at position 0, 0, 0.

If you want it in the direction of the part, from the hitbox, with magnitude 8, then do

local AreaRay = Ray.new(Hitbox.Position, (Part.Position - Hitbox.Position).Unit * 8)
4 Likes

Alrighty! It’s working now, Thanks!

I actually had seen a solution to this before, but i accidently switched Hitbox with Part
without noticing and forgot to add .Unit. :sweat_smile:

1 Like