Simplest way to restrict movement to a certain axis?

Like the title already says, I’m currently trying to restrict the players movement to a certain axis (in my case I only want the player to move sideways)

I’ve already come across a few solutions but they either don’t work on all devices or are rather complicated (such as using BodyGyros/BodyVelocities and then manually moving the player when the A/D key was pressed)

Does anyone know a good solution for this problem?
(PS: Unbinding the W/S button isn’t really an option because movement depends on the cameras angle.

You can literally just subtract the world assemblylinearvelocity to constraint it on a certain axis.

The simplest way is to use the plane constraint.

Simply create an attachment in terrain then a plane constraint.

the Attachment 0 would be the terrain attachment and attachment 1 Would be a constraint in the character e.g. RootRigConstraint.

Rotate the attachment in terrain to move the plane.

If you do want you character to move up and down then you will want to use a prismatic constraint instead.

2 Likes

I would manually reposition the player every physics step:

-- in StarterCharacterScripts
local character = script.Parent
local zPosition = 0 -- will force the player to Z = 0
game:GetService("RunService").Heartbeat:Connect(function()
  local root = character:FindFirstChild("HumanoidRootPart")
  if root then
    root.CFrame = root.CFrame + Vector3.new(0, 0, zPosition - root.Position.Z)
  end
end)

This avoids any physics weirdness with using a constraint, but still allows for the normal roblox control scripts to run.

8 Likes