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

How to measure the Distance of the Part?

Asked by
KAAK82 16
10 years ago

I want to know how to measure the distance the part flew/went for example, Bullet; when reached range <= 100 then do damage - 10. that was just an example of the thing I wanted to know...

1 answer

Log in to vote
1
Answered by 10 years ago

You would do something like this:

local Bullet = script.Parent --Change this to the path of the bullet
local Active = true
coroutine.resume(coroutine.create(function()
    local StartPos = Bullet.Position
    while true do
        if (Bullet.Position - StartPos).magnitude > 100 then --If the distance between the StartPos and the Bullet is > 100...
            Active = false
            break
        end
        wait()
    end
end))
Bullet.Touched:connect(function(Obj)
    if Active then --If the bullet hasn't reached a range of > 100...
        --Do stuff
    elseif (not Active) then --If the bullet has reached a range of > 100...
        --Do stuff
    end
end)

EDIT This is a much simpler version that LightArceus suggested:

local Bullet = script.Parent --Change this to the path of the bullet
local StartPos = Bullet.Position
Bullet.Touched:connect(function(Obj)
    local BulletDist = (Bullet.Position - StartPos).magnitude
    if BulletDist > 100 then --If the bullet hasn't reached a range of > 100...
        --Do stuff
    elseif BulletDist <= 100 then --If the bullet has reached a range of > 100...
        --Do stuff
    end
end)
0
Wouldn't it be more efficient to put the distance checker in the touched function? That way you wouldn't have to use a loop. LightArceus 110 — 10y
0
thnx for the help guys, but I want to check the distance more than once so as for more realistic damage KAAK82 16 — 10y
Ad

Answer this question