How can I know the distance between two part?

I want to do a system like ‘Door’, so when you go near to a door, the door open, i have try to understand on youtube but I don’t find great result.

Thank you!

use Magnitude

local Mag = (Part1.Position - Part2.Position).Magnitude

if Mag <= 12 then 
print("Part2 is near Part1")
end
9 Likes

thank you I think is that but I can test now, I’m not at my home.
I didn’t think anyone would answer me so fast, sorry! :sweat_smile:

1 Like

Instead of constantly calculating the distance it would be way easier and less performance heavy to just use ZonePlus. Basically, when the player enters the zone near the door, it opens and the next room generates, this is what ive done in my own game I made that was based on Doors.

2 Likes

Or you can just make a hitbox part slightly bigger than the door and use the Touched event.

2 Likes

Magnitude check is always going to be more performant than zonePlus. It’s a O(1) operation vs a spatial query

2 Likes

I wouldn’t recommend the .Touched event because from my own experience I always found it really inconsistent.

1 Like

If your doing it for the player
use .touched for only the hrp this has given me the best results! and use a debounce lol