Hello, I have been working on the acceleration based movement mechanic for half a year, and at the last moment when I thought I have fixed all the bugs, it comes to another problem.
When a character is on the high ground and is about to fall, I am trying to keep it’s speed midair until the character stepped on the ground. For some reason, when I walk to the edge and fall down, the speed acclelrates to the value equalent to WalkSpeed. I checked every value and found no problem with it
local player = game:GetService('Players').LocalPlayer
local UIS = game:GetService('UserInputService')
local character = player.Character
local humanoid = character:WaitForChild('Humanoid')
local rootPart = character:WaitForChild('HumanoidRootPart')
local crouching = false
local jumping = false
run.RenderStepped:Connect(function(deltaTime)
--3rd person camera
if humanoid.Health > 0 then
local _, y = camera.CFrame.Rotation:ToEulerAnglesYXZ()
rootPart.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(0, y, 0)
end
--Body-to-Camera rotation
local camDirection = camera.CFrame.LookVector
bodyCamModifier.bodyCamMod(player, camDirection)
bodyModifier:FireServer(camDirection)
--Acceleration modify
if control:GetMoveVector().Magnitude > 0 then
crouching = false
moveSpeed = .5
else
crouching = false
moveSpeed = 0
end
if UIS:IsKeyDown(Enum.KeyCode.LeftControl) then
crouching = true
moveSpeed = .3
elseif UIS:IsKeyDown(Enum.KeyCode.LeftShift) and not crouching and not aiming then
crouching = false
moveSpeed = 1
end
if jumping then
--Humanoid.WalkSpeed is 22
xAccelerate = rootPart.Velocity.X / 22
zAccelerate = rootPart.Velocity.Z / 22
else
xAccelerate += (humanoid.MoveDirection.X * moveSpeed - xAccelerate) * deltaTime * 5
zAccelerate += (humanoid.MoveDirection.Z * moveSpeed - zAccelerate) * deltaTime * 5
end
humanoid:Move(Vector3.new(math.round(xAccelerate * 100) / 100, 0, math.round(zAccelerate * 100) / 100), false)
end)
humanoid.StateChanged:Connect(function(old, new)
if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
jumping = true
else
jumping = false
end
end)
Is there any way to fix this? I appreciate all helps.
Are you trying to make a movement system where walking down an inclined slope would cause the player to accelerate until they hit a speed cap (That speed being the WalkSpeed) , and they would stay at that speed while falling down a slope, but their speed would reset to normal when they land on the ground?
If so, do you wish for this system to allow players to slide down slopes and use this to reach a speed higher than the WalkSpeed (For example going up a slope and then sliding down and gaining a speed higher than the WalkSpeed).
No, I do not intend the player to slide down the slope. I am mentioning the bug when you fall from the height without jumping which accelerates the player.
I just have a thought, could you use VectorForces? If so, that would take a lot of math and calculation off your back and it would be faster since it doesnt operate on the Lua layer, but rather internal via the Physics engine.
For it, you just need to set it to one attachment mode, add an attachment to the HumanoidRootPart, set the attachment, and adjust the Force property.
Unless friction is removed, VectorForce is unstable. GIving the force as Vector3.new(10000, 0, 0), it moves sluggish while on the ground, but uncontrollably fast while jumping.
Sorry if my math is wrong, but maybe you could take Friction into consideration and create a new force value such that when friction is applied, it will result in the wanted force.
I guess you could find the friction coefficient via currentphysicalproperties but I’m not sure.
Ff (Friction force)
Fu (Friction coefficient)
N (Original force/Force set in VectorForce)
Nd (Desired force/Resultant force)
Since Ff = Fu * N, and the resultant would be Nd = N - Ff, we can substitute Ff into the formula:
Nd = N - Fu * N
We can factorize this into:
Nd = N * ( 1 - Fu )
To get the original force,
Nd/(1 - Fu) = N
So I suppose you’d substitute into here the friction coefficient of the part the player is standing on and the force you want to apply.
Sorry if I wasted your time reading this and if it doesn’t solve your issue/isn’t what you want. Also sorry if my math is off I just looked up the friction equations.
I did not use VectorForce at the first time cause I would be afraid that the friction would ruin the code, but it works now! Guess I have to get used with this method.
Yeah, I suppose it’s kind of annoying to do, but it does mean that the physics simulation is handled by Roblox making it more stable, more resilient to updates, and faster/efficient/more performant since it is done by machine code (in C/C++) rather than in a Lua VM.