Player's speed varies when player is in different angles!

Hello devs! I’m trying to replicate source engine movement, but I’ve noticed something weird, when I turn in different angles like when you turn forwards your speed is normal but when you turn left you go extremely fast, here’s some code that I think make the problem :

-- Server variables
local sv = {
	accelerate = 5.5,
	airAccelerate = 12,
	maxVelocity = 3500,
	airMaxVelocity = 3500,
	friction = 1 --5.2
}
-- Movement functions
local function accelerate(accelerationDirection: Vector3,previousVelocity: Vector3,accel,maxVelocity,dt)
	local projectedVelocity = previousVelocity:Dot(accelerationDirection)
	local accelerationVelocity = accel * dt
	
	if (projectedVelocity + accelerationVelocity) > maxVelocity then
		accelerationVelocity = maxVelocity - projectedVelocity
	end
	
	return previousVelocity + accelerationDirection * accelerationVelocity
end
local function moveGround(accelerationDirection: Vector3,previousVelocity: Vector3,dt)
	local speed = previousVelocity.Magnitude
	if speed ~= 0 then
		local drop = speed * sv.friction * dt
	end
	return accelerate(accelerationDirection,previousVelocity,sv.accelerate * 100,sv.maxVelocity,dt)
end
local function moveAir(accelerationDirection: Vector3,previousVelocity: Vector3,dt)
	return accelerate(accelerationDirection,previousVelocity,sv.airAccelerate * 100,sv.airMaxVelocity,dt)
end
-- In RenderStepped
if moveDirection.Magnitude > 0 then
  -- The part where speed changes
 	local velo = Vector3.new()
 		
 	local wishDir = moveDirection.Unit
 		
 	local accel = 0
 
 	if onGround() then
 		velo = moveGround(wishDir,previousVelocity,dt)
 	else
 		velo = moveAir(wishDir,previousVelocity,dt)
 	end
 		
 	velo = camera.CFrame:VectorToWorldSpace(velo)
 		
 	accel = velo.Magnitude
 		
 	humanoid.WalkSpeed = accel
 		
 	newVelocity = velo.Unit
end
velocity = velocity:Lerp(newVelocity,0.4)
	
humanoid:Move(velocity,false)
	
previousVelocity = newVelocity

Any help would be appreciated!

1 Like