#Jittery Movement when using AssemblyLinearVelocity for movement

1 messages · Page 1 of 1 (latest)

vast elk
#

I made a local script to move the player and it works, but for some reason the movement is very jittery (See video). Any clue what the issue is? Not sure if this is the best way to do it.

Movement Script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local player = Players.LocalPlayer

local MOVE_FORWARD = "MoveForward"
local MOVE_BACKWARD = "MoveBackward"
local MOVE_SPEED = 750

local forwardPressed = false
local backwardPressed = false


function moveForward(_, inputState, _)
    if inputState == Enum.UserInputState.Begin then
        forwardPressed = true
    elseif inputState == Enum.UserInputState.End then
        forwardPressed = false
    end
end

function moveBackward(_, inputState, _)
    if inputState == Enum.UserInputState.Begin then
        backwardPressed = true
    elseif inputState == Enum.UserInputState.End then
        backwardPressed = false
    end
end

ContextActionService:BindAction(MOVE_FORWARD, moveForward, true, Enum.KeyCode.W)
ContextActionService:BindAction(MOVE_BACKWARD, moveBackward, true, Enum.KeyCode.S)

player.CharacterAdded:Connect(function()
    local tank = player.Character
    local root = tank.PrimaryPart

    RunService.RenderStepped:Connect(function(delta)
        local speedVector = root.CFrame.RightVector * MOVE_SPEED * delta
        local velocity = Vector3.new()

        if forwardPressed and not backwardPressed then
            velocity = speedVector
        elseif backwardPressed and not forwardPressed then
            velocity = -speedVector
        end

        root.AssemblyLinearVelocity = Vector3.new(velocity.X, root.AssemblyLinearVelocity.Y, velocity.Z)
    end)
end)
fossil nestBOT
#

studio** You are now Level 1! **studio

vast elk
#

The blue part of the model is smooth at the bottom (negated part + sphere) and the grey part is massless with cancollide off, so I doubt the issue is due to the model (I may be wrong)

vocal bronze
#

use this:``` local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local player = Players.LocalPlayer
local MOVE_SPEED = 750

local inputState = {
forward = false,
backward = false
}

local activeConnections = {}

local function handleMovementInput(actionName, inputState, _)
local isPressed = inputState == Enum.UserInputState.Begin

if actionName == "MoveForward" then
inputState.forward = isPressed
elseif actionName == "MoveBackward" then
inputState.backward = isPressed
end
end

-- Vincular acciones
ContextActionService:BindAction("MoveForward", handleMovementInput, true, Enum.KeyCode.W)
ContextActionService:BindAction("MoveBackward", handleMovementInput, true, Enum.KeyCode.S)

player.CharacterAdded:Connect(function(character)
local root = character:WaitForChild("HumanoidRootPart")

if activeConnections.renderStep then
activeConnections.renderStep:Disconnect()
end

activeConnections.renderStep = RunService.RenderStepped:Connect(function(delta)
local velocity = Vector3.new()

if inputState.forward and not inputState.backward then
  velocity = root.CFrame.LookVector * MOVE_SPEED
elseif inputState.backward and not inputState.forward then
  velocity = -root.CFrame.LookVector * MOVE_SPEED
end

root.AssemblyLinearVelocity = Vector3.new(velocity.X, root.AssemblyLinearVelocity.Y, velocity.Z)

end)

character:WaitForChild("Humanoid").Died:Connect(function()
if activeConnections.renderStep then
activeConnections.renderStep:Disconnect()
end
end)
end)```

#

Unified function - handleMovementInput handles both actions
Connection disconnection - Prevents memory leaks when the character changes
LookVector instead of RightVector - For correct forward/backward movement
WaitForChild - Waits for HumanoidRootPart to exist
Cleaner structure - Table to store active connections
No unnecessary delta - Speed ​​is already in units/second

vast elk
#

bro thinks he's chatgpt 🙏

vast elk
# vocal bronze Unified function - handleMovementInput handles both actions Connection disconnec...

for 1 2 and 5, I plan to clean up the code once i've got it working without issues

I used rightvector instead of lookvector since the direction I want it to move forward in is to the body's right facing direction

There is no humanoid root part in the model and using waitforchild on the primary part doesnt fix the issue

I used delta so there wouldn't be variations in speed at different framrates, removing it doesnt fix the jitter

#

this is the model's hierarchy

reef sonnetBOT
vast elk
#

oops

vocal bronze
#

Instead of setting the velocity directly, you use a constraint that the physics engine respects natively:

#

-- When creating the character/vehicle, add the constraint once

local linearVelocity = Instance.new("LinearVelocity", root)
linearVelocity.Attachment0 = attachment
linearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
linearVelocity.PrimaryTangentAxis = Vector3.new(1, 0, 0) -- adjust to the correct axis
linearVelocity.SecondaryTangentAxis = Vector3.new(0, 0, 1)
linearVelocity.MaxForce = math.huge
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World

-- In the loop
RunService.Heartbeat:Connect(function(delta)
local targetVelocity = Vector3.zero
if forwardPressed and not backwardPressed then
    targetVelocity = root.CFrame.RightVector * MOVE_SPEED
elseif backwardPressed and not forwardPressed then
    targetVelocity = -root.CFrame.RightVector * MOVE_SPEED
end
linearVelocity.PlaneVelocity = Vector2.new(targetVelocity.X, targetVelocity.Z)
end)```