Finding the depth of a part that was raycast through

I’m currently developing an Open Source anti exploit module. At the moment, I raycast from all the players’ last known positions to their current positions every half second. The following is a visualization of a false flag noclip attempt that the anti exploit picked up:

image

The red line is the direction of the ray, with the white sphere being the cast start point, the red being the contact point, and the gold sphere being the end point. Is there a method to calculate what length of the ray the part in question occupies? I’m trying to ignore shallow noclip pickups as they’re almost certainly people just cornering walls.

2 Likes

You can use the Distance method on the ray in question. :slight_smile:

Bit confused. What position do i give the distance method?

You seem to have the red sphere also. If you have got the ray unit then you can check for hit position with normal unit from white sphere (so you have this as red sphere) and another ray (from yellow sphere) but with inverted unit so you will detect two positions where the surface is and after you can just calculate their magnitude.
Simplified example:
local MainRay = Ray.new(start, (end-start)) local BackRay = Ray.new(end, (start, end)) -- inverted one local hit, position, normal = game.Workspace:FindPartOnRay(MainRay) local invertedhit, invertedposition, invertednormal = game.Workspace:FindPartOnRay(BackRay) local depth = (position-invertedposition).magnitude

2 Likes

Cast another ray going the other direction. The endpoint would be the hit position of the first ray, the origin would be the hit position + the direction of the original ray times the length of the part’s longest diagonal. The depth would the the distance between the two hits.

-- Casts a ray with a given origin and direction (direction includes the ray's length!)
-- Returns nil if there was nothing hit,
-- or the hit part, hit position, and the depth.
local function raycastWithDepthChecking(origin, direction)
    local hit, pos = workspace:FindPartOnRay(Ray.new(origin, direction))
    if hit then
        local diagonalLength = hit.Size.magnitude
        local reverseLook = -direction.unit
        local rDirection = reverseLook*diagonalLength
        local rOrigin = hit - rDirection
        local reverseRay = Ray.new(rOrigin, rDirection)
        local _, rPos = workspace:FindPartOnRayWithWhitelist(reverseRay, { hit })
        local depth = (rPos - pos).magnitude
        return hit, pos, depth
    else
        return nil
    end
end
5 Likes

Yeah, I think you can just raycast from the start point and back from the end point, then consider the two surfaces that were hit. You could probably get pretty sophisticated about this, like checking the alignment of the surface normals; if they’re pointing away from each other (normal1:Dot(normal2) < 0) then you can guess they walked right through a part.

I would be really careful about implementing a system like this, though. You really don’t want any false positives.

7 Likes

If the endpoint of the original ray happens to land inside the part itself, then you won’t hit anything. Your second raycast should also only have the original part hit as it’s whitelist, just in case there is another part behind the original part.

1 Like

if endpoint actually ends in the part - the player is also in the part so its easy to calculate - he is surely exploiting.

2 Likes