I set up a movement controller using LinearVelocity. The issue is that if you move forward and reach top speed, but then input a different direction, like forward + left, the speed decreases for a moment because the maximum velocity is constrained by the magnitude of the movement vector. If I understand the docs correctly, setting LinearVelocity.ForceLimitMode to PerAxis (and changing other relevant LinearVelocity properties) should fix this. However, if I change these properties the LinearVelocity stops working entirely, and the player movement reverts to default Roblox movement.
My code looks like this:
local RunService = game:GetService("RunService")
local Players = game:GetService('Players')
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local rootAttachment: Attachment = root:WaitForChild("RootAttachment")
local playerModel = character
local humanoid = playerModel:WaitForChild("Humanoid")
local linearVelocity = Instance.new("LinearVelocity", root)
linearVelocity.Attachment0 = rootAttachment
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
linearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
linearVelocity.PrimaryTangentAxis = Vector3.new(1, 0, 0)
linearVelocity.SecondaryTangentAxis = Vector3.new(0, 0, 1)
linearVelocity.MaxForce = 100_000
-- adding these lines seems to disable the linearvelocity.
linearVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
linearVelocity.ForceLimitsEnabled = true
linearVelocity.MaxPlanarAxesForce = Vector2.new(32, 32)
function update(_, dt)
local origin = root.Position
local inputDirection = Vector2.new(humanoid.MoveDirection.X, humanoid.MoveDirection.Z)
local targetVelocity
local deltaVelocity
local accelerationRate
targetVelocity = Vector2.new(inputDirection.X, inputDirection.Y) * 32
deltaVelocity = targetVelocity - linearVelocity.PlaneVelocity
accelerationRate = if inputDirection.Magnitude > 0 then 8 else 4
linearVelocity.PlaneVelocity += deltaVelocity * accelerationRate * dt
character:SetAttribute("speed", Vector2.new(linearVelocity.PlaneVelocity.X, linearVelocity.PlaneVelocity.Y).Magnitude)
end
RunService.Stepped:Connect(update)