Hello there! I’m making skydive system for my game, and I need to make when you fly down you will gain speed and when up the speed will decrease and when you will hit speed 0 the fly will cancel, Anyway I don’t know how could I achieve that
Because I don’t really know how to detect if player is going up or down!
Checking to see if the last Y position is less than the current literally means that they are going up. If they fly up, the Y increases. If they go down, the Y decreases. Compare it like this:
if lastY > Character.HumanoidRootPart.Position.Y then
print("We are falling. The last Y position was higher, and we are going down.")
end
spawn is highly unreliable. Use coroutine instead. Also, while wait(x) loops are bad practice. Lets try something like this:
local lastY = nil
local canUpdate = true
local function setLastYValue() -- Make an "infinite" loop with renderstepped.
while true do
if not canUpdate then return end
canUpdate = false
lastY = Character.HumanoidRootPart.Position.Y
print(lastY)
wait(.1)
canUpdate = true
end
end
local newThread = coroutine.wrap(setLastYValue) -- Set up a new thread. Basically what spawn does.
newThread()
Also, instead of using loops, you could just update it when the position changes.
local lastY = nil
Character.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
if Character.HumanoidRootPart.Position.Y ~= lastY and Character.HumanoidRootPart.Position.Y < lastY then
lastY = Character.HumanoidRootPart.Position.Y
end
end)
local lastY = nil
local speedSound = -- path to sound
Character.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
if Character.HumanoidRootPart.Position.Y ~= lastY and Character.HumanoidRootPart.Position.Y < lastY then
lastY = Character.HumanoidRootPart.Position.Y
local speed = Character.HumanoidRootPart.Velocity.magnitude
speedSound.PlaybackSpeed = ((speed / 100) * 2)
end
end)
If the sound sounds weird, try changing it from times 2 to something else like times 1.5 etc