-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFreecam_mobile.lua
More file actions
114 lines (103 loc) · 3.39 KB
/
Freecam_mobile.lua
File metadata and controls
114 lines (103 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local onMobile = not UIS.KeyboardEnabled
local keysDown = {}
local rotating = false
if not game:IsLoaded() then game.Loaded:Wait() end
cam.CameraType = Enum.CameraType.Scriptable
local speed = 30
local sens = .3
speed /= 10
if onMobile then sens*=2 end
local function renderStepped()
if rotating then
local delta = UIS:GetMouseDelta()
local cf = cam.CFrame
local yAngle = cf:ToEulerAngles(Enum.RotationOrder.YZX)
local newAmount = math.deg(yAngle)+delta.Y
if newAmount > 65 or newAmount < -65 then
if not (yAngle<0 and delta.Y<0) and not (yAngle>0 and delta.Y>0) then
delta = Vector2.new(delta.X,0)
end
end
cf *= CFrame.Angles(-math.rad(delta.Y),0,0)
cf = CFrame.Angles(0,-math.rad(delta.X),0) * (cf - cf.Position) + cf.Position
cf = CFrame.lookAt(cf.Position, cf.Position + cf.LookVector)
if delta ~= Vector2.new(0,0) then cam.CFrame = cam.CFrame:Lerp(cf,sens) end
UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
else
UIS.MouseBehavior = Enum.MouseBehavior.Default
end
if keysDown["Enum.KeyCode.W"] then
cam.CFrame *= CFrame.new(Vector3.new(0,0,-speed))
end
if keysDown["Enum.KeyCode.A"] then
cam.CFrame *= CFrame.new(Vector3.new(-speed,0,0))
end
if keysDown["Enum.KeyCode.S"] then
cam.CFrame *= CFrame.new(Vector3.new(0,0,speed))
end
if keysDown["Enum.KeyCode.D"] then
cam.CFrame *= CFrame.new(Vector3.new(speed,0,0))
end
end
RS.RenderStepped:Connect(renderStepped)
local validKeys = {"Enum.KeyCode.W","Enum.KeyCode.A","Enum.KeyCode.S","Enum.KeyCode.D"}
UIS.InputBegan:Connect(function(Input)
for i, key in pairs(validKeys) do
if key == tostring(Input.KeyCode) then
keysDown[key] = true
end
end
if Input.UserInputType == Enum.UserInputType.MouseButton2 or (Input.UserInputType == Enum.UserInputType.Touch and UIS:GetMouseLocation().X>(cam.ViewportSize.X/2)) then
rotating = true
end
if Input.UserInputType == Enum.UserInputType.Touch then
if Input.Position.X < cam.ViewportSize.X/2 then
touchPos = Input.Position
end
end
end)
UIS.InputEnded:Connect(function(Input)
for key, v in pairs(keysDown) do
if key == tostring(Input.KeyCode) then
keysDown[key] = false
end
end
if Input.UserInputType == Enum.UserInputType.MouseButton2 or (Input.UserInputType == Enum.UserInputType.Touch and UIS:GetMouseLocation().X>(cam.ViewportSize.X/2)) then
rotating = false
end
if Input.UserInputType == Enum.UserInputType.Touch and touchPos then
if Input.Position.X < cam.ViewportSize.X/2 then
touchPos = nil
keysDown["Enum.KeyCode.W"] = false
keysDown["Enum.KeyCode.A"] = false
keysDown["Enum.KeyCode.S"] = false
keysDown["Enum.KeyCode.D"] = false
end
end
end)
UIS.TouchMoved:Connect(function(input)
if touchPos then
if input.Position.X < cam.ViewportSize.X/2 then
if input.Position.Y < touchPos.Y then
keysDown["Enum.KeyCode.W"] = true
keysDown["Enum.KeyCode.S"] = false
else
keysDown["Enum.KeyCode.W"] = false
keysDown["Enum.KeyCode.S"] = true
end
if input.Position.X < (touchPos.X-15) then
keysDown["Enum.KeyCode.A"] = true
keysDown["Enum.KeyCode.D"] = false
elseif input.Position.X > (touchPos.X+15) then
keysDown["Enum.KeyCode.A"] = false
keysDown["Enum.KeyCode.D"] = true
else
keysDown["Enum.KeyCode.A"] = false
keysDown["Enum.KeyCode.D"] = false
end
end
end
end)