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

How would I check if a part is within a sphere (ball) with magnitude?

Asked by
qChaos 86
5 years ago

I am trying to figure out how to check if a part is within a ball part (sphere) using magnitude, but I can't figure out the correct math. Any help would be appreciated heavily.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I recommend using GetTouchingParts, using Magnitude has more math in it, GetTouchingParts is simpler and easier, magnitude takes up a few extra lines...

local p = workspace.Part.Position.Magnitude
local p2 = workspace.OtherPart.Position.Magnitude

if math.abs(p-p2)<p-workspace.Part.Size.X then -- a sphere size has to be the same for x,y,z I believe.
     print('colliding')
end
-- I really don't know if this will work or not, looks fine to me though

I am using abs because sometimes it glitches for me, It is believed that magnitudes can never be negative but I really don't know.

You didn't give code so I didn't give precise results, if this helps though, click answer. c:

0
I am using this for damaging players in a sphere explosion, and using gettouchingparts on a very big explosion is an issue (yes I tested), so that is why I am asking qChaos 86 — 5y
0
I ended up figuring this out myself, but thanks for trying to help! qChaos 86 — 5y
0
Good job!! greatneil80 2647 — 5y
Ad
Log in to vote
0
Answered by
qChaos 86
5 years ago
local Sphere=workspace.Sphere
local Part=workspace.Part
if (Part.Position-Sphere.Position).Magnitude<=Sphere.Size.Magnitude/3 then
    print'Part is Inside of Sphere!'
end

Answer this question