I am using a first person script from the toolbox to make it possible to view the persons body in first person mode. Sadly this script makes my game impossible to be played on mobile devices: on mobile devices the camera just cannot be rotated. It can be moved by walking around but not otherwise. The script is below, as I said it’s open sourced. It would be nice if anyone could help me with this!
Edit: I do not need you to fix the script for me, I am just asking for help on where the issue could be.
repeat wait() until script.Parent.Parent:IsA("Player")
repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")
CanToggleMouse = {allowed = false; activationkey = Enum.KeyCode.F;} -- lets you move your mouse around in firstperson
CanViewBody = true -- whether you see your body
Sensitivity = 0.2 -- anything higher would make looking up and down harder; recommend anything between 0~1
Smoothness = 0.05 -- recommend anything between 0~1
FieldOfView = 90 -- fov
local cam = game.Workspace.CurrentCamera
local player = players.LocalPlayer
local m = player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388" -- replaces mouse icon
local character = player.Character or player.CharacterAdded:wait()
local humanoidpart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")
local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0
local running = true
local freemouse = false
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
function updatechar()
for i, v in pairs(character:GetDescendants()) do
if v:IsA("ParticleEmitter") then
v:Destroy()
end
end
for i, v in pairs(character.Head:GetDescendants()) do
if v:IsA("Decal") then
v:Destroy()
end
end
for _, v in pairs(character:GetChildren())do
if CanViewBody then
if v.Name == 'Head' then
v.LocalTransparencyModifier = 1
v.CanCollide = false
end
else
if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
v.LocalTransparencyModifier = 1
v.CanCollide = false
end
end
if v:IsA'Accessory' then
v:FindFirstChild('Handle').LocalTransparencyModifier = 1
v:FindFirstChild('Handle').CanCollide = false
end
if v:IsA'Hat' then
v:FindFirstChild('Handle').LocalTransparencyModifier = 1
v:FindFirstChild('Handle').CanCollide = false
end
end
end
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
input.InputChanged:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness
local X = TargetAngleX - delta.y
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (TargetAngleY - delta.x) %360
end
end)
input.InputBegan:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.KeyCode == CanToggleMouse.activationkey then
if CanToggleMouse.allowed and freemouse == false then
freemouse = true
else
freemouse = false
end
end
end
end)
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
runService.RenderStepped:connect(function()
if running then
updatechar()
CamPos = CamPos + (TargetCamPos - CamPos) *0.28
AngleX = AngleX + (TargetAngleX - AngleX) *0.35
local dist = TargetAngleY - AngleY
dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist
AngleY = (AngleY + dist *0.35) %360
cam.CameraType = Enum.CameraType.Scriptable
cam.CoordinateFrame = CFrame.new(head.Position)
* CFrame.Angles(0,math.rad(AngleY),0)
* CFrame.Angles(math.rad(AngleX),0,0)
* CFrame.new(0,0.8,0) -- offset
humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
character.Humanoid.AutoRotate = false
else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default; character.Humanoid.AutoRotate = true
end
if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then
running = false
else
running = true
if freemouse == true then
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
else
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
end
end
if not CanToggleMouse.allowed then
freemouse = false
end
cam.FieldOfView = FieldOfView
end)
The script currently relies heavily on detecting mouse movement (UserInputType.MouseMovement) and keyboard input (UserInputType.Keyboard). These inputs are not directly applicable to mobile devices, which use touchscreens instead of mice and lack physical keyboards for input. The script dynamically controls camera position and orientation based on these inputs, which would not function correctly on mobile devices due to the absence of mouse movement and keyboard input for toggling camera modes.
To make the script compatible with mobile devices, extensive changes would be necessary to switch to UserInputType.Touch for detecting touchscreen interactions. This would involve implementing touch controls for camera movement, such as swiping gestures for rotation and potentially adjusting the camera view based on device orientation sensors. As suggested by @finaIlyhappy starting from scratch with a Roblox tutorial designed for mobile compatibility would be a more effective approach.
repeat wait() until script.Parent.Parent:IsA("Player")
repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")
CanToggleMouse = {allowed = false; activationkey = Enum.KeyCode.F;} -- lets you move your mouse around in firstperson
CanViewBody = true -- whether you see your body
Sensitivity = 0.2 -- anything higher would make looking up and down harder; recommend anything between 0~1
Smoothness = 0.05 -- recommend anything between 0~1
FieldOfView = 90 -- fov
local cam = game.Workspace.CurrentCamera
local player = players.LocalPlayer
local m = player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388" -- replaces mouse icon
local character = player.Character or player.CharacterAdded:wait()
local humanoidpart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")
local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p
local AngleX,TargetAngleX = 0,0
local AngleY,TargetAngleY = 0,0
local running = true
local freemouse = false
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
function updatechar()
for i, v in pairs(character:GetDescendants()) do
if v:IsA("ParticleEmitter") then
v:Destroy()
end
end
for i, v in pairs(character.Head:GetDescendants()) do
if v:IsA("Decal") then
v:Destroy()
end
end
for _, v in pairs(character:GetChildren())do
if CanViewBody then
if v.Name == 'Head' then
v.LocalTransparencyModifier = 1
v.CanCollide = false
end
else
if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
v.LocalTransparencyModifier = 1
v.CanCollide = false
end
end
if v:IsA'Accessory' then
v:FindFirstChild('Handle').LocalTransparencyModifier = 1
v:FindFirstChild('Handle').CanCollide = false
end
if v:IsA'Hat' then
v:FindFirstChild('Handle').LocalTransparencyModifier = 1
v:FindFirstChild('Handle').CanCollide = false
end
end
end
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
input.InputChanged:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness
local X = TargetAngleX - delta.y
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (TargetAngleY - delta.x) %360
end
if inputObject.UserInputType == Enum.UserInputType.Touch then
local delta = Vector2.new(inputObject.Delta.x / Sensitivity, inputObject.Delta.y / Sensitivity) * Smoothness
local X = TargetAngleX - delta.y
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (TargetAngleY - delta.x) % 360
end
end)
input.InputBegan:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then
if inputObject.KeyCode == CanToggleMouse.activationkey then
if CanToggleMouse.allowed and freemouse == false then
freemouse = true
else
freemouse = false
end
end
end
end)
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
runService.RenderStepped:connect(function()
if running then
updatechar()
CamPos = CamPos + (TargetCamPos - CamPos) *0.28
AngleX = AngleX + (TargetAngleX - AngleX) *0.35
local dist = TargetAngleY - AngleY
dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist
AngleY = (AngleY + dist *0.35) %360
cam.CameraType = Enum.CameraType.Scriptable
cam.CoordinateFrame = CFrame.new(head.Position)
* CFrame.Angles(0,math.rad(AngleY),0)
* CFrame.Angles(math.rad(AngleX),0,0)
* CFrame.new(0,0.8,0) -- offset
humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
character.Humanoid.AutoRotate = false
else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default; character.Humanoid.AutoRotate = true
end
if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then
running = false
else
running = true
if freemouse == true then
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
else
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
end
end
if not CanToggleMouse.allowed then
freemouse = false
end
cam.FieldOfView = FieldOfView
end)