hook.Add("KeyPress", "DoubleJump", function(pl, k)
if not pl or not pl:IsValid() or k~=2 then
return
end
if not pl:HasPurchase("doublejump") then return end
if not pl.Jumps or pl:IsOnGround() then
pl.Jumps=0
end
if pl.Jumps==2 then return end
pl.Jumps = pl.Jumps + 1
if pl.Jumps==2 then
local ang = pl:GetAngles()
local forward, right = ang:Forward(), ang:Right()
local vel = -1 * pl:GetVelocity() -- Nullify current velocity
vel = vel + Vector(0, 0, 300) -- Add vertical force
local spd = pl:GetMaxSpeed()
if pl:KeyDown(IN_FORWARD) then
vel = vel + forward * spd
elseif pl:KeyDown(IN_BACK) then
vel = vel - forward * spd
end
if pl:KeyDown(IN_MOVERIGHT) then
vel = vel + right * spd
elseif pl:KeyDown(IN_MOVELEFT) then
vel = vel - right * spd
end
pl:SetVelocity(vel)
end
end)
смысл в -1 * pl:GetVelocity(). Можно же просто изменить .z параметр.
Не нужно полностью сбрасывать вектор игрока и потом заново его просчитывать ( заново просчитывать каждое нажатие кнопки и прибавлять velocity ) “Nullify current velocity”
Вместо ~магических векторов~ Vector(0, 0, 300) используй :GetJumpPower().
Не используй ~магические цыфры~ если есть константы. (k != 2) Код становится нечитабельным. Чем плох IN_JUMP
Движок не будет запускать хук KeyPress если игрок вышел с сервера. Нет смысла в первой проверке
Улучшенный код по моему мнению:
local maxJumps = 1
local IN_JUMP = IN_JUMP
hook.Add("KeyPress", "DoubleJump", function(pl, k)
if not pl:HasPurchase("doublejump") then return end
if pl:IsOnGround() then
pl.Jumps = 0
return
end
if k ~= IN_JUMP then
return
end
pl.Jumps = pl.Jumps + 1
if pl.Jumps <= maxJumps then
local vel = Vector( 0, 0, 0 )
vel.z = -pl:GetVelocity().z + pl:GetJumpPower( )
pl:SetVelocity( vel )
end
end)
Также стоит ипользовать хуки SetupMove , CreateMove, FinishMove, Move, ...
Они специально созданы чтобы изменять движение игрока. Чтобы не было “лага”