I have a 2-D game, and when players contact uneven parts, they shift across the Z-axis. To solve this issue, I made a Zed locking script in StarterCharacterScripts that keeps the players’ X and Y-axes the same but changes the Z to be aligned with the 2-D world’s center.
local plr = game.Players.LocalPlayer
local char = plr.Character
while true do
task.wait(1)
char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.X, char.HumanoidRootPart.CFrame.Y, workspace.SpawnSpot.CFrame.Z)
end
It works, but the player also gets faced backwards. I don’t want it to change their orientation. What should I do?
Thank you, I appreciate the help! This makes the player now rotate looking to the left, though.
local plr = game.Players.LocalPlayer
local char = plr.Character
while true do
task.wait(1)
local pos = Vector3.new(char.HumanoidRootPart.CFrame.X, char.HumanoidRootPart.CFrame.Y, workspace.SpawnSpot.CFrame.Z)
char.HumanoidRootPart.CFrame = CFrame.new(pos, char.HumanoidRootPart.CFrame.LookVector)
end
local plr = game.Players.LocalPlayer
local char = plr.Character
while true do
task.wait(1)
char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(char.HumanoidRootPart.CFrame.X, char.HumanoidRootPart.CFrame.Y, workspace.SpawnSpot.CFrame.Z)
end
You can preserve the rotational component of the CFrame while defining a new position.
-- in a RunService.Heartbeat connection
local oldCFrame = humanoidRootPart.CFrame
local newPosition = Vector3.new(oldCFrame.X, oldCFrame.Y, workspace.SpawnSpot.Position.Z)
humanoidRootPart.CFrame = oldCFrame.Rotation + newPosition
Plr = game.Players.LocalPlayer
Char = Plr.Character or Plr.CharacterAdded:Wait()
while wait() do
local E = workspace.Part.CFrame.LookVector -- might be used
Char:MoveTo(Vector3.new(0,Char.HumanoidRootPart.Position.Y, Char.HumanoidRootPart.Position.Z)) -- change it if you like
game:GetService("RunService").RenderStepped:Wait()
end
Client Movement Replicates to the Server
I would unbind some keys using ContextActionService if you don’t want to limit their movement