I am trying to make a NumberValue smoothly increase or decrease between 1 and 0.001 based on the distance between two objects. Once they are a certain distance apart, the value remains at 0.001, and once they are within close proximity the value remains at 1.
I am having trouble finding anything having to do with smoothly transitioning between values based on distance, so I figured I would make a post about it. All I have right now is a while loop that checks and prints the magnitude of the two parts every 2 seconds.
I have a few ideas for messy solutions I could fuss over and scrap together, but I feel that would be an excuse not to learn something, and I feel that a clean solution that requires more complex knowledge is the better option.
I am a beginner scripter, and I have been away from Roblox for a long time, so my beginner skills are a bit rusty. This probably seems like a simple problem, which is the only explanation I can think of for why I haven’t seen this issue posted anywhere else. I would really appreciate some insights on what I should research and learn for things like this, and ideas for how to achieve this effect.
local MAX_DISTANCE = 20 --//Anything outside of this range results in a scalar of 0
local function getScalar(dist) --//Takes the distance as parameter. Returns a scalar (value from 0-1 that scales the maximum value)
if dist < MAX_DISTANCE then
return (MAX_DISTANCE - dist) / MAX_DISTANCE --//The closer you get to the object, the larger the scalar should become, still ranging from 0-1 though
else
return 0
end
end
I haven’t tested this, but it should be pretty close to what you want. You can specify a maximum distance to use with the scaling, and then you could use the scalar returned to multiply a value.
To further configure this, you could clamp the value so it does not fall below a certain value. (e.g: you want it to remain at 0.1 and not 0. You can clamp it so the value will always remain above 0, like the code below)
local value = math.clamp(getScalar(dist), 0.001, 1)
Thanks a ton! It worked on the first attempt. Here’s the script.
local goal = script.Parent
local rock = game.Workspace:FindFirstChild("Boulder")
local TimeSpeed = game:GetService("ReplicatedStorage").TimeValue
local Maxdistance = 200
local function getScalar(distance)
if distance < Maxdistance then
return (Maxdistance - distance) / Maxdistance
else
return 0
end
end
while true do
wait()
local distance = (rock.Position - goal.Position).Magnitude
TimeSpeed.Value = math.clamp(getScalar(distance), 0.001, 1)
end
While it works perfectly, I would really appreciate a source for the math that’s going on here (max distance - distance)/ max distance, because I don’t have a clue as to what this is actually doing. I really want to understand this instead of just pasting it in. I looked up what a scalar was, and that provided a tiny bit of insight, but I still don’t know what’s happening here (this is probably the first time I have ever been excited about math haha).
Thanks again, this formula will probably be useful in a lot of ways for future projects, and I really appreciate the help.