Hey there!
I am making 2D Platformer game. I keep facing an issue with the control system, whenever I go too far from spawn, my character just disappears
My camera manipulation local script:
local camera = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local rs = game:GetService("RunService")
camera.CameraType = Enum.CameraType.Scriptable
function update()
local character = plr.Character or plr.CharacterAdded:Wait()
if not character then return end
local hrp = character:WaitForChild("HumanoidRootPart")
if not hrp then return end
camera.CFrame = CFrame.new(hrp.CFrame.X, hrp.CFrame.Y, hrp.CFrame.Z) * CFrame.new(-20, 0, 0) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(0))
end
rs:BindToRenderStep("Camera", Enum.RenderPriority.Input.Value, update)
My custom movement local script:
local plr = game.Players.LocalPlayer
local rs = game:GetService("RunService")
local cas = game:GetService("ContextActionService")
local jumping = false
local leftVal = 0
local rightVal = 0
function left(actionName, inputState)
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
if inputState == Enum.UserInputState.Begin then
leftVal = humanoid.WalkSpeed/16
elseif inputState == Enum.UserInputState.End then
leftVal = 0
end
end
function right(actionName, inputState)
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
if inputState == Enum.UserInputState.Begin then
rightVal = humanoid.WalkSpeed/16
elseif inputState == Enum.UserInputState.End then
rightVal = 0
end
end
function jump(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
jumping = true
elseif inputState == Enum.UserInputState.End then
jumping = false
end
end
function update()
local character = plr.Character or plr.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
local moveVal = rightVal - leftVal
humanoid:MoveTo(Vector3.new(hrp.CFrame.X, hrp.CFrame.Y, hrp.CFrame.Z + moveVal))
if jumping then
humanoid.Jump = true
if leftVal == 0 and rightVal == 0 then return end
local linVel = Instance.new("LinearVelocity", hrp)
linVel.Attachment0 = hrp.VelAttachment
if leftVal > rightVal then
linVel.LineDirection = Vector3.new(0, 0, -1)
elseif rightVal > leftVal then
linVel.LineDirection = Vector3.new(0, 0, 1)
end
linVel.VelocityConstraintMode = Enum.VelocityConstraintMode.Line
linVel.LineVelocity = 30
wait(0.1)
linVel:Destroy()
end
end
rs:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, update)
cas:BindAction("Left", left, true, "a", Enum.KeyCode.A, Enum.KeyCode.DPadLeft)
cas:BindAction("Right", right, true, "d", Enum.KeyCode.D, Enum.KeyCode.DPadRight)
cas:BindAction("Jump", jump, true, " ", Enum.KeyCode.Space, Enum.KeyCode.DPadUp)```