Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I check if a part is colliding with another part? (Anchored parts)

Asked by 10 years ago

I'm trying to figure out an accurate way to determine if an Anchored part is touching any other part. Here is the function I have written so far.

function checkCollisions(part)
    local MIN,MAX,RAN,RAD,FLOOR,ROOF = math.min,math.max,math.random,math.rad,math.floor,math.ceil
    local pos = part.Position
    local size = part.Size
    local startpos = Vector3.new(pos.x-size.x/2,pos.y-size.y/2,pos.z-size.z/2)
    local endpos = Vector3.new(pos.x+size.x/2,pos.y+size.y/2,pos.z+size.z/2)
    local REG3 = Region3.new(Vector3.new(MIN(startpos.X,endpos.X),MIN(startpos.Y,endpos.Y),MIN(startpos.Z,endpos.Z)),Vector3.new(MAX(startpos.X,endpos.X),MAX(startpos.Y,endpos.Y),MAX(startpos.Z,endpos.Z)))
    local partsinREG3 = game.Workspace:FindPartsInRegion3(REG3,nil,1)
    if #partsinREG3 > 0 and partsinREG3[1].Transparency == 0 then
        return partsinREG3[1]
    elseif #partsinREG3 < 1 then
        print(part.Name..' is not colliding with any other object.')
        return false
    end
end

It seems fine, but for some reason, it bugs out and sometimes says that their is a part in range when there isn't.

0
I need someone that can tell me how to fix this function because I'm having a good amount of trouble myself. xolbStudios 127 — 10y

Answer this question