# Player Acceleration

Hey there!

I’ve recently gotten into Lua scripting and I’ve been aiming to create a freerunning-style gamemode (nothing fancy to be honest).

However, I’ve been trying to get player acceleration, such that the player slowly picks up speed until he reaches the maximum speed he can travel. This is my code so far:

[lua]function PlayerRunning(ply,movedata)
if ply:OnGround() then
Speed = Speed + 1.5
if Speed > Maxspeed then
Speed = Maxspeed
end
end
GAMEMODE:SetPlayerSpeed(ply,Speed,Speed)
end

If I die, and then respawn, I slowly gain speed till I reach 400, however, I stop, the next time I start moving, I instantly reach 400…

Just wondering if someone can give me help or point me into the right direction!

-p64

Try something like this:

if ply:KeyDown(IN_FORWARD) and ply:OnGround() then
ply.Speed = math.Clamp((ply.Speed or 0) + 1.5, 0, Maxspeed)
else
ply.Speed = math.Clamp((ply.Speed or 0) - 10, 0, Maxspeed)
end
movedata:SetVelocity(ply:GetForward():GetNormal()*ply.speed)
end)[/lua]

Use this, but make it use the hook **[Move

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.Move)** and make it shared instead. This way clients can predict it, making it much smoother.

Isn’t SetupMove shared?

[editline]12:55PM[/editline]

Move seems better anyway.

I’ve copied and pasted MakeR’s code, overwriting my code, and when I tested it, I get:

``````

Hook 'SlowAcceleration' Failed: includes/extensions/math.lua:67: attempt to compare nil with number

``````

Any ideas?

include(“shared.lua”)
time = CurTime()
Speed = 0
Maxspeed = 700[/lua]

That’s what I use to define the varibles used; would I have to do a for loop to allocate a seperate ‘speed’ value for each player?

Just add ply.Speed = 0 in a PlayerInitialSpawn hook.

Sorry, my mistake. In my code MaxSpeed should be Maxspeed.

[editline]01:08PM[/editline]

Nope, Already have that covered with (ply.Speed or 0)

if ply:KeyDown(IN_FORWARD) and ply:OnGround() then
ply.Speed = math.Clamp((ply.Speed or 0) + 1.5, 0, Maxspeed)
else
ply.Speed = math.Clamp((ply.Speed or 0) - 10, 0, Maxspeed)
end
movedata:SetVelocity(ply:GetForward():GetNormal()*ply.Speed)
end)[/lua]

This should work.

[editline]03:10PM[/editline]

:ninja:

[editline]03:11PM[/editline]

You were also multiplying ply:GetForward():GetNormal() with speed, which was nil. Should’ve been ply.Speed.

Bah.

Thanks for pointing that out.

Okay, well I managed to get it working; It was giving me issues that the character falls at a speed of 9, when walking forward, he goes forward by 30 and when sprinting it’s 46 (I have a speedometer on using: “math.Round(client:GetVelocity():Length())”). But, now if I jump, he just jolts up (with a speed of 9) and then floats down, yet running still increasing. Any thoughts?

Remove the ply:OnGround() part of it.

No luck; I removed the ply:OnGround() and the character still floats (very slowly) to the ground. Wouldn’t I have to somehow incorporate the character’s Z velocity aswell with SetVelocity?

[lua]hook.Add( “Move”, “AccelerationThingie”, function( ply, mv )
local speed = ply:GetVelocity()
local norm = speed:GetNormal()

``````ply.Speed = ply.Speed or 0
if ply:KeyDown( IN_FORWARD ) then
if ply:OnGround() then
ply.Speed = ply.Speed + 1.5
end
else
ply.Speed = ply.Speed - 5
end
ply.Speed = math.Clamp( ply.Speed, 0, Maxspeed )
mv:SetVelocity( Vector( norm.x * ply.Speed, norm.y * ply.Speed, speed.z ) )
``````

end )[/lua]

This one should solve the jumping problem.

I placed that into the code, and the jumping working, but now the character runs at like 2500 and on really weird angles

Wouldn’t it be eaiser to just add a Vector(0,0,-1) to the speed.z and use the previous calculations for the movement?

Edited the fixed version in the above post.

Now the character accelerates, but when I’m running, I can’t really turn; I can be facing right, while pressing forward, and he will still run in the same direction as before, and I can only turn if I look like 90 degrees from the direction he’s running…