Changing CFrame without rotating

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?

I recommend including the character’s LookVector when assigning the new CFrame

locak 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)
1 Like

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
Ex.CFrame = EX.CFrame * CFrame.new() -- This will only change the Position
Ex.CFrame = EX.CFrame * CFrame.Angles() -- This will only change Orientation
1 Like

Thank you, like this?

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

This should work, if not, try saving the Values of the Orientation and applying them as they go

1 Like

It teleports me to a random spot in the sky, not sure why.

Yeah, line 14 error. Once again, really appreciate the help though!

local plr = game.Players.LocalPlayer
local char = plr.Character
local HumanoidRootPart = char.HumanoidRootPart
PosX = HumanoidRootPart.CFrame.Position.X
PosY = HumanoidRootPart.CFrame.Position.Y
PosZ = HumanoidRootPart.CFrame.Position.Z
Look = workspace.SpawnSpot.CFrame.LookVector

while true do
	task.wait(1)
	local Look = workspace.SpawnSpot.CFrame.LookVector
	HumanoidRootPart.CFrame = CFrame.new(PosX, PosY, PosZ)

	HumanoidRootPart.CFrame.Orientation = Look -- Error: Orientation cannot be assigned to
	-- within Loop

	
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
1 Like

I managed to do this:

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

1 Like

Thank you so much, it works like a charm! All of you are life savers!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.