# Help with gliding mechanic

I have this piece of code here that increases and decreases the gliding speed of the player when they’re flying based on how much they’re going up and down. The problem with this is, if you glide upwards at the right angle, the player can just keep gliding upwards for as long as they want; that doesn’t happen, realistically, with a glider.

``````local perDown = direction.Unit.Y

velocity.Force += direction * 100 * (1 - perDown) * HRP.AssemblyMass
``````

What I’m aiming for is allowing players to speed up as they dive down, but then slow back down once they go against gravity, like if they go up or are no longer diving down.

For example, if a player dives down and then quickly glides up, they’ll retain that speed going up but they’ll slow down until they can no longer go upwards (cuz gravity big and speed too small), so they’re forced to glide back down. This mechanic can be seen in Bird Simulator - Roblox and Glider Simulator [WORLD 2] - Roblox

How can I do this?

3 Likes

Yo, I been askin this for a while now, but I have not gotten a good answer. lol.

I would also like to do this, but I kinda gave up on my game that would use it already.

1 Like

The snippet you included seems to speed up players based on angle, but with no possibility of slowing them down, unless I’m missing something.

Each time the code runs, it adds an amount of force that scales with the direction the player points, but the math there means that force will never be negative. Is there drag or something not included here that fixes that, or am I misunderstanding the code?

oh there’s drag. The only issue lies in the code I provided, and you’re right. It speeds up players based on angle. After trial and error, I know that the speed doesn’t need to be negative; it only needs to be less than the force of gravity so that stops going up. At least, that’s my understanding of it.

Here’s the drag tho

``````if HRP.AssemblyLinearVelocity.Magnitude > 0 then
local dragVector = -HRP.AssemblyLinearVelocity.Unit * HRP.AssemblyLinearVelocity.Magnitude ^ DRAG_POWER
velocity.Force += dragVector * DRAG_COEFFICIENT * HRP.AssemblyMass
end
``````

yeah, I think it’s going to take my a long time until I find a solution

1 Like

So to avoid the infinite-flight issue, can you offset the maximum speed gained by a constant amount?

It sounds like the problem occurs when

``````direction * 100 * (1 - perDown) * HRP.AssemblyMass > (GravityForce + DragForce)
``````

when the player is pointing up at a shallow angle, which lets them gain speed while gliding up. Can you correct it by changing the (1-perDown) line to say, (0.5-perDown)? Or if that’s too drastic, maybe a number in between until it feels good?

This is basically what I’m doing:

``````local GRAVITY = Vector3.new(0, game.Workspace.Gravity, 0)

local function onGlide()
velocity.Force = (GRAVITY/1.05 * HRP.AssemblyMass)

if humanoid_direction.Magnitude > 0 and direction.Magnitude > 0 then
local perDown = direction.Unit.Y
velocity.Force += direction * 100 * (1 - perDown) * HRP.AssemblyMass
end

if HRP.AssemblyLinearVelocity.Magnitude > 0 then
local dragVector = -HRP.AssemblyLinearVelocity.Unit * HRP.AssemblyLinearVelocity.Magnitude ^ DRAG_POWER
velocity.Force += dragVector * DRAG_COEFFICIENT * HRP.AssemblyMass
end
end
``````

And the problem would occur when

``````velocity.Force.Y > (GRAVITY * HRP.AssemblyMass).Y
--velocity includes all the 3 values u see for velocity in the onGlide function added up
``````

ur solutions was one of the things I tried. It doesn’t work. I put .75 for example, I can still find the angle to go up forever. If I do .5 for example, I also won’t be able to glide the way I want like in the game I linked. It doesn’t retain momentum if I were to dive down, but then glide back up, and then do it again

Here’s a clip with (.5 - perDown): https://i.gyazo.com/c87843e40cf40310158422e2192cefc7.mp4
I just sink to the ground after diving, not being able to glide up. Same thing happens if I do (.75 - perDown)

1 Like