Convert distance to percentage

Hello,

I need help converting the distance between two objects to a percentage.

I have two objects, one stationary (A), and another that moves ( B). When B is less than a maximum distance to A (in this case 200), something starts to happen to A. But what I also want is within that radius for a percentage to be calculated, with 0% being maximum distance and 100% being on top of object A. So the percentage of A increases the closer you get.

Any help would be greatly appreciated.

Thanks,

Dan

local percent = 1 - ( currentDistance / maxDistance )

Rob

Perfect.

As always, thanks Rob.

I should have been clear.  That will give you a value of 0 … 1 where 1 is 100%.  If you want to display that as a percentage, multiply the number by 100 to convert it to a percentage. 

I actually wanted a value of 0 to 1. So that worked out great.

Thanks again.

local percent = 1 - ( currentDistance / maxDistance )

Rob

Perfect.

As always, thanks Rob.

I should have been clear.  That will give you a value of 0 … 1 where 1 is 100%.  If you want to display that as a percentage, multiply the number by 100 to convert it to a percentage. 

I actually wanted a value of 0 to 1. So that worked out great.

Thanks again.

function GetPercentLeftInTravelPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = (1 - ( currentDistance / distanceToTravel)) * 100
   return tempnumber
end
print ("GetPercentLeftInTravelPath: " … GetPercentLeftInTravelPath(67.2,100) … “%”)

function GetPercentTravelledAlongPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = 100 * ( currentDistance / distanceToTravel)
   return tempnumber
end
print ("GetPercentTravelledAlongPath: " … GetPercentTravelledAlongPath(45.1,100) … “%”)

And using percentages to get distances where the portion travelled and total to travel is known

function GetDistanceLeftToTravelPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = totalPathDistance - ((percentTravelled / 100) * totalPathDistance)
    end
   return tempdist
end
print ("GetDistanceLeftToTravelPath: " … GetDistanceLeftToTravelPath(33,1000))

function GetDistanceTravelledOnPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = (percentTravelled / 100) * totalPathDistance
    end
   return tempdist
end
print ("GetDistanceTravelledOnPath: " … GetDistanceTravelledOnPath(33,1000))

These are useful. You should bundle them up and add them to the community code.

Rob

function GetPercentLeftInTravelPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = (1 - ( currentDistance / distanceToTravel)) * 100
   return tempnumber
end
print ("GetPercentLeftInTravelPath: " … GetPercentLeftInTravelPath(67.2,100) … “%”)

function GetPercentTravelledAlongPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = 100 * ( currentDistance / distanceToTravel)
   return tempnumber
end
print ("GetPercentTravelledAlongPath: " … GetPercentTravelledAlongPath(45.1,100) … “%”)

And using percentages to get distances where the portion travelled and total to travel is known

function GetDistanceLeftToTravelPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = totalPathDistance - ((percentTravelled / 100) * totalPathDistance)
    end
   return tempdist
end
print ("GetDistanceLeftToTravelPath: " … GetDistanceLeftToTravelPath(33,1000))

function GetDistanceTravelledOnPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = (percentTravelled / 100) * totalPathDistance
    end
   return tempdist
end
print ("GetDistanceTravelledOnPath: " … GetDistanceTravelledOnPath(33,1000))

These are useful. You should bundle them up and add them to the community code.

Rob