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

How to detect if a Model is overlapped with another object?

Asked by 7 years ago

I have some model, and they have random position, so I need to validate if the model is overlapped with another object (like player, car or also the same cloned model), but I can't figure out how to validate if the model is overlapped with another object, and validate is if that overlay is acceptable, because not all overlays are unacceptable.

You may be asking a very big question, but I'm a bit confused and I do not even know where to begin with this algorithm.

Any help would be appreciated.

1 answer

Log in to vote
1
Answered by
cabbler 1942 Moderation Voter
7 years ago

Very accurately? You'll have to check every part in the model yourself. Generally? Use the functions Model:GetExtentsSize() and Model:GetModelCFrame() to create a relevant Region3 .

Like this:

wait(1)

local model = workspace:WaitForChild('Model') --path to any model

--model info
local size = model:GetExtentsSize()
local hsize = size/2
local pos = model:GetModelCFrame().p

--parameters for region
local lowp = pos - hsize
local highp = pos + hsize

local r3 = Region3.new(lowp,highp)

--optional warning box
local box = Instance.new('Part')
box.BrickColor = BrickColor.new('Really red')
box.Transparency=1
box.Anchored=true
box.CanCollide=false
box.CFrame = r3.CFrame
box.Size = r3.Size
box.Parent = workspace

--creating a manual ignore list is useful
local ignoreList = {}
--igore parts in the model
function fun(x) if x:IsA('BasePart') then table.insert(ignoreList,x) end end function scan(y) fun(y) for _,v in pairs(y:GetChildren()) do scan(v) end end scan(model)
--ignore parts such as baseplate
for _,v in pairs(workspace:GetChildren()) do if v:IsA('BasePart') then table.insert(ignoreList,v) end end

--in example:
while wait(1) do
    --most useful function!
    local parts = workspace:FindPartsInRegion3WithIgnoreList(r3,ignoreList,1000)
    if #parts>0 then
        box.Transparency=0.5
    else
        box.Transparency=0.9
    end
end
0
I'ts is not exactly what I am loking for, because this code asume that the CFrame of the PrimaryPart is the center of the model, but also I see in code line 8 "model:GetModelCFrame().p", the function :GetModelCFrame() does not appears in the api, but I think I can modifie the code to get what I'm looking for LordSalchipapas 96 — 7y
0
Thanks so mush LordSalchipapas 96 — 7y
0
Hmm either briefly toggle the PrimaryPart or center it. I don't think there's a better way to get the center of a model. cabbler 1942 — 7y
0
I did not know that this method exists, in fact to invest days making a code to get the center of a model, this information does not appear in the API LordSalchipapas 96 — 7y
0
btw I am not sure how you would collision detect another Region but OldPalHappy's comment might help. cabbler 1942 — 7y
Ad

Answer this question