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

How to check how many studs away a part is?

Asked by 8 years ago

How could I check how many studs away a part is from another part. Such as, a part named Ice. And another named cream, if Ice got 10 studs away from cream, it would print "stop" Any ideas?

2 answers

Log in to vote
1
Answered by 8 years ago

To find the distance between two points, you can use the following equation:

Distance = math.sqrt((PX1 - PX2)^2 + (PY1 - PY2)^2 + (PZ1 - PZ2)^2)

So, for your case, this might look like:

local Pos1 = Ice.Position
local Pos2 = Cream.Position

local Distance = math.sqrt((Pos1.X - Pos2.X)^2 + (Pos1.Y - Pos2.Y)^2 + (Pos1.Z - Pos2.Z)^2)

if Distance < 10 then
    print("stop")
end

This would need to be run on a loop or connected to an event, but it should give you an idea how to achieve this.

1
Why not just use magnitude? TheHospitalDev 1134 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago

It won't let me post comments so I am just gonna post what my idea is,

You could make it so if it got (0, 5, 0) away from it than it will print stop,

That should be easy though, I am not fantastic but somewhere along lines like

if Ice.position = Vector3 (0, 5, 0)
then
print ("stop")

I expect to lose more points from this because it isn't a comment but this is all I can do for you, Sorry if it is wrong, I didn't make you a correct script I made you a code to represent my idea.

Answer this question