Hello,
Long story short, i need to detect if object 1 (red ball) is in object 2. (grey ball)
i tried to use Region3 but it does not work for spheres…
Is there any way to detect if objects are in each other?
Hello,
Long story short, i need to detect if object 1 (red ball) is in object 2. (grey ball)
i tried to use Region3 but it does not work for spheres…
Is there any way to detect if objects are in each other?
Yes, with the parent.
local Object1 -- Red Ball
local Object2 -- Grey Ball
if Object1.Parent == Object2 then
return true
else
print(Object1.Parent.Name .." is the parent's name.")
return false
end
If that doesn’t work, just do this.
local Object1
local Name -- Put the name of the second object
if Object1.Parent.Name == Name then
return true
else
print(Object1.Parent.Name .." is the parent's name.")
return false
end
Oh, did you mean with parent/child or with the position?
They are both in workspace, i want to detect if they are in eachother and when they are.
EDIT: this code up can be usefull, thank you i will take it.
i think you can achieve this using collision groups. Collision Filtering | Documentation - Roblox Creator Hub
You can use touched and a boolvalue for that.
Like this;
local Bool
local Object1
local Name -- Second Object's Name
Object1.Touched:Connect(function(Hit)
if Hit.Name == Name then
Bool.Value = true
end
end)
You could check if the red ball is located in some radius from the center of the grey ball.
Please mark this post as a solution if this helped
You can use :GetTouchingParts()
:
local Object1
local Object2
for i,v in pairs(Object1:GetTouchingParts()) do
if v = Object2 then
--Code here
end
end
I think that can work but i think it would take too much procesing power, if i run out of ideas that can help.
i will test it, give me minute
Adding onto that, I think he means use magnitude. you can use:
local redBall = game.Workspace.RedBall
local greyBall = game.Workspace.greyBall
local distance = (redBall.Position - greyBall.Position).magnitude
if distance == 0 then
-- code
end
Magnitude checks the distance between 2 CFrames. This script only checks once, so you’ll have to use a loop of some kind. Note, if you dont know what magnitude is, it’s basically the distance between 2 cframes. I used an if to detect if the distance was 0, meaning it was in the center, so you should probably do if distance <= 5. 5 is how many studs away from the center the object is
i get this error
12:06:46.341 GetTouchingParts is not a valid member of Model “Workspace.Object1” - Server - Script:3
Wait Object1 is a model?
I thought it was a basepart.
it is basepart, but it says its model for some reason
local greyBall = game.Workspace.greyBall
local distance = (redBall.Position - greyBall.Position).magnitude
if distance => 0 and distance < greyBallSize```
-- code
end
Can you send a screenshot of the object explorer?
my bad, there was model in workspace called Object1
Now all works, thank you
ok, thank you evryone, i will take all your ideas and make 2 stage detection system with GetTouchingParts and magnitude calculations.
Cheers from Serbia!