my custom character controller for a sphere character works perfectly in studio, it has very smooth and fast movement, but when i play in-game on the roblox app it slows me down!!!
i turned it into a starterplayerscript and it didnt work
local uis = game:GetService("UserInputService")
local run = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = player:GetMouse()
local moveDir = Vector3.zero
local force = 15000
local runMultiplier = 2
local isRunning = false
local jumpForce = 7000
local maxSpeed = 300
local damping = 0.92
local canJump = true
local jumpCooldown = 0.5
local isMobile = uis.TouchEnabled
local heartbeatConnection = nil
local ball = nil
local KEYS = "LeftControl,RightControl"
local mouseLockController = player.PlayerScripts:WaitForChild("PlayerModule"):WaitForChild("CameraModule"):WaitForChild("MouseLockController")
local obj = mouseLockController:FindFirstChild("BoundKeys")
if obj then
obj.Value = KEYS
else
obj = Instance.new("StringValue")
obj.Name = "BoundKeys"
obj.Value = KEYS
obj.Parent = mouseLockController
end
local function setupBall(newBall)
if heartbeatConnection then
heartbeatConnection:Disconnect()
heartbeatConnection = nil
end
ball = newBall
if not ball then return end
pcall(function()
ball:SetNetworkOwnershipAuto()
end)
cam.CameraSubject = ball
cam.CameraType = Enum.CameraType.Custom
heartbeatConnection = run.Heartbeat:Connect(function(dt)
if not ball or not ball.Parent then return end
if not isMobile then
moveDir = Vector3.zero
if uis:IsKeyDown(Enum.KeyCode.W) then moveDir += Vector3.new(0, 0, 1) end
if uis:IsKeyDown(Enum.KeyCode.S) then moveDir += Vector3.new(0, 0, -1) end
if uis:IsKeyDown(Enum.KeyCode.A) then moveDir += Vector3.new(-1, 0, 0) end
if uis:IsKeyDown(Enum.KeyCode.D) then moveDir += Vector3.new(1, 0, 0) end
isRunning = uis:IsKeyDown(Enum.KeyCode.LeftShift) or uis:IsKeyDown(Enum.KeyCode.RightShift)
end
if moveDir.Magnitude > 0 then
local look = cam.CFrame.LookVector
local right = cam.CFrame.RightVector
local camMove = (right * moveDir.X) + (look * moveDir.Z)
camMove = Vector3.new(camMove.X, 0, camMove.Z).Unit
local currentForce = force * (isRunning and runMultiplier or 1)
ball:ApplyImpulse(camMove * currentForce * dt * moveDir.Magnitude)
else
local velocity = ball.AssemblyLinearVelocity
local horizontalVel = Vector3.new(velocity.X, 0, velocity.Z)
if horizontalVel.Magnitude < 5 then
local ray = cam:ScreenPointToRay(mouse.X, mouse.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {ball}
local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 1000, raycastParams)
local targetPos
if raycastResult then
targetPos = raycastResult.Position
else
targetPos = ray.Origin + ray.Direction * 1000
end
local direction = (targetPos - ball.Position).Unit
local verticalAngle = math.asin(direction.Y)
verticalAngle = math.clamp(verticalAngle, math.rad(-40), math.rad(100))
local clampedY = math.sin(verticalAngle)
local horizontalScale = math.cos(verticalAngle)
local horizontalDir = Vector3.new(direction.X, 0, direction.Z).Unit
local clampedDirection = Vector3.new(horizontalDir.X * horizontalScale, clampedY, horizontalDir.Z * horizontalScale)
local targetCFrame = CFrame.lookAt(ball.Position, ball.Position + clampedDirection)
ball.CFrame = ball.CFrame:Lerp(targetCFrame, 0.1)
end
end
local velocity = ball.AssemblyLinearVelocity
local horizontalVel = Vector3.new(velocity.X, 0, velocity.Z)
if horizontalVel.Magnitude > 0 then
local dampedVel = horizontalVel * damping
if horizontalVel.Magnitude > maxSpeed then
dampedVel = dampedVel * 0.8
end
ball.AssemblyLinearVelocity = Vector3.new(dampedVel.X, velocity.Y, dampedVel.Z)
end
local vel = ball.AssemblyLinearVelocity
local horizontalVel2 = Vector3.new(vel.X, 0, vel.Z)
if horizontalVel2.Magnitude > maxSpeed/4 then
ball.CustomPhysicalProperties = PhysicalProperties.new(0.7, 0.6, 1, 1, 100)
else
ball.CustomPhysicalProperties = PhysicalProperties.new(0.7, 0.6, 0, 1, 100)
end
end)
end
if isMobile then
local ui = script.movement:Clone()
ui.Parent = player:WaitForChild("PlayerGui")
ui.Enabled = true
local jumpButton = ui:WaitForChild("button")
local runButton = ui:WaitForChild("run")
local joystickBase = ui:WaitForChild("joystick")
local joystickStick = joystickBase:WaitForChild("stick")
joystickStick.Active = false
jumpButton.MouseButton1Down:Connect(function()
if canJump and ball then
ball:ApplyImpulse(Vector3.new(0, jumpForce, 0))
canJump = false
task.delay(jumpCooldown, function()
canJump = true
end)
end
end)
runButton.MouseButton1Down:Connect(function()
isRunning = true
runButton.BackgroundColor3 = Color3.fromRGB(100, 200, 100)
end)
runButton.MouseButton1Up:Connect(function()
isRunning = false
runButton.BackgroundColor3 = Color3.fromRGB(0,0,0)
end)
local joystickActive = false
local joystickTouch = nil
local joystickCenter = Vector2.zero
local maxJoystickDistance = joystickBase.AbsoluteSize.X / 2
joystickBase.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then
joystickActive = true
joystickTouch = input
local basePos = joystickBase.AbsolutePosition
local baseSize = joystickBase.AbsoluteSize
joystickCenter = basePos + (baseSize / 2)
end
end)
uis.InputEnded:Connect(function(input)
if input == joystickTouch then
joystickActive = false
joystickTouch = nil
moveDir = Vector3.zero
joystickStick.Position = UDim2.new(0.5, 0, 0.5, 0)
end
end)
uis.InputChanged:Connect(function(input)
if joystickActive and input == joystickTouch then
local touchPos = input.Position
local delta = Vector2.new(touchPos.X, touchPos.Y) - joystickCenter
local distance = delta.Magnitude
if distance > maxJoystickDistance then
delta = delta.Unit * maxJoystickDistance
distance = maxJoystickDistance
end
local stickOffset = delta / joystickBase.AbsoluteSize.X
joystickStick.Position = UDim2.new(0.5 + stickOffset.X, 0, 0.5 + stickOffset.Y, 0)
local magnitude = distance / maxJoystickDistance
if magnitude > 0.1 then
local angle = math.atan2(delta.Y, delta.X)
moveDir = Vector3.new(math.cos(angle), 0, -math.sin(angle)) * magnitude
else
moveDir = Vector3.zero
end
end
end)
end
uis.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.Space and canJump and ball then
ball:ApplyImpulse(Vector3.new(0, jumpForce, 0))
canJump = false
task.delay(jumpCooldown, function()
canJump = true
end)
end
end)
player.CharacterAdded:Connect(function(character)
local newBall = character:WaitForChild("Head", 5)
if newBall then
setupBall(newBall)
end
end)
if player.Character then
local initialBall = player.Character:WaitForChild("Head", 5)
if initialBall then
setupBall(initialBall)
end
end
ingame:
External Mediain studio: