Value that increases wihen you get closer to an object

Currently I am trying to figure out a way to make the wings on a meter gradually go up the closer you get to a part and when you get farther away the wings gradually go down, I currently am thinking on it skipping to frames of an animation but I was wondering if any of you on dev forum would have any idea of how to accomplish something like this. I have been using stuff like Magnitude and using stuff similar to AI scripts, but I just wanted to hear others opinions on something like this.

Not copy and pasted from the actual script, but this is similar to what I was considering when scripting it:

local Body = script.Parent
local PrimaryPart

for i,v in pairs(game.Workspace:GetChildren())
local GhostT = v:FindFirstChild("GhostTorso")
if GhostT then
PrimaryPart = GhostT.Parent.PrimaryPart
if (PrimaryPart.Position - Body.Position).Magnitude <= 50 then
Distance = (PrimaryPart.Position - Body.Position).Magnitude
if Distance <= 25 then
--Animation Stuff here
else
--Animation go backwards or something
local Body = script.Parent
local PrimaryPart

for i,v in pairs(game.Workspace:GetChildren())
local GhostT = v:FindFirstChild("GhostTorso")
if GhostT then
PrimaryPart = GhostT.Parent.PrimaryPart
local Distance = (PrimaryPart.Position - Body.Position).Magnitude
if Distance <= 50 then
  if Distance <= 25 then
    animation:Play(1)
  else
    animation:Play(-1)
  end
end

First argument of AnimationTrack:Play() is playbackSpeed.

Alright, I’ll keep that in mind. Thank you.

Do you know of any methods that can be used to gradually make it go up the closer you get to an object? Like any already made forums and such discussing it that I can read up and study off of.

Can you explain what exactly should go up?

Yeah, my bad for not specifying lol

image

I already have an animation made where the wings go up, so I was thinking if there was a way that overtime, the closer you get to an object the wings in turn would start rising, but if you stop then the wings would stop rising and if you go back and farther from the object they start lowering.

Edit: The wings being the side parts, but as I said I already have an animation that raises them

I might of misunderstood the question but you can play around with animationTrack.TimePosition, here’s a dodgy demo code I’ve made where the animation progresses as I get closer to the part:

local len = animationTrack.Length
local MIN = 10
local MAX = 30
local RS = game:GetService("RunService")
while RS.RenderStepped:Wait() do
	local mag = (workspace.Part.Position - character.HumanoidRootPart.Position).Magnitude
	if mag < MIN then
		animationTrack:Play()
		animationTrack:AdjustSpeed(0)
		animationTrack.TimePosition = len
	elseif mag < MAX then
		print("animation at "..100-100*((mag-MIN)/(MAX-MIN)).."%")
		animationTrack:Play()
		animationTrack:AdjustSpeed(0)
		animationTrack.TimePosition = (1/len)*((mag-MIN)/(MAX-MIN))
	end
end

Video:

External Media

That’s exactly what I was looking for! I am testing it right now, animation isn’t playing but I bet I can get that all working. Thank you so much for the assistance, I really appreciate it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.