I have this 2D game, in which I obviously do not want the players to go to a different X axis.
When I ragdoll, the following happens:
So I tried making something to lock it in the axis (the following function runs on RenderStepped):
local function OnUpdate()
local Character = LocalPlayer.Character
if Character and Character:FindFirstChildOfClass("Humanoid") then
local Force = Character.PrimaryPart:FindFirstChildOfClass("VectorForce")
if Force == nil then
local Att = Instance.new("Attachment", Character.PrimaryPart)
Force = Instance.new("VectorForce")
Force.ApplyAtCenterOfMass = true
Force.Force = Vector3.new(0, 8700, 0)
Force.Attachment0 = Att
Force.RelativeTo = Enum.ActuatorRelativeTo.World
Force.Parent = Character.PrimaryPart
end
local movementDirection = Left - Right
local moveVector = Vector3.new(movementDirection, 0, 0)
local currentPosition = Character.PrimaryPart.CFrame.Position
if Character["Humanoid"].FloorMaterial ~= Enum.Material.Air then
if movementDirection ~= 0 then
lastX = movementDirection
end
Character.Humanoid:Move(moveVector, false)
end
if lastX ~= 0 and Character["Humanoid"]:GetState() ~= Enum.HumanoidStateType.Physics then
Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(currentPosition.X, currentPosition.Y, -2), currentPosition + Vector3.new(lastX, 0, 0)))
elseif Character["Humanoid"]:GetState() == Enum.HumanoidStateType.Physics then --it is ragdolled
Character:SetPrimaryPartCFrame(CFrame.new(Character.PrimaryPart.CFrame.Position.X, Character.PrimaryPart.CFrame.Position.Y, -2))
end
end
end
Which, makes this:
Which is obviously not the desired result.
I would like to know how to make this work. Thank you for your time, in advance!