Advertisement
CloneTrooper1019

iOS Controls

Apr 13th, 2014
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.14 KB | None | 0 0
  1. -- This is responsible for all touch controls we show (as of this writing, only on iOS)
  2. -- this includes character move thumbsticks, and buttons for jump, use of items, camera, etc.
  3. -- Written by Ben Tkacheff, Copyright Roblox 2013
  4.  
  5. -- obligatory stuff to make sure we don't access nil data
  6. while not Game do
  7.         wait()
  8. end
  9. while not Game:FindFirstChild("Players") do
  10.         wait()
  11. end
  12. while not Game.Players.LocalPlayer do
  13.         wait()
  14. end
  15. while not Game:FindFirstChild("CoreGui") do
  16.         wait()
  17. end
  18. while not Game.CoreGui:FindFirstChild("RobloxGui") do
  19.         wait()
  20. end
  21.  
  22. local userInputService = Game:GetService("UserInputService")
  23. local success = pcall(function() userInputService:IsLuaTouchControls() end)
  24. if not success then
  25.         script:Destroy()
  26. end
  27.  
  28. ----------------------------------------------------------------------------
  29. ----------------------------------------------------------------------------
  30. -- Variables
  31. local screenResolution = Game:GetService("GuiService"):GetScreenResolution()
  32. function isSmallScreenDevice()
  33.         return screenResolution.y <= 320
  34. end
  35.  
  36. local localPlayer = Game.Players.LocalPlayer
  37. local thumbstickInactiveAlpha = 0.3
  38. local thumbstickSize = 120
  39. if isSmallScreenDevice() then
  40.         thumbstickSize = 70
  41. end
  42. local thumbstickInnerImage = "rbxasset://textures/ui/thumbstickInner.png"
  43. local thumbstickOuterImage = "rbxasset://textures/ui/thumbstickOuter.png"
  44. local ThumbstickDeadZone = 5
  45. local ThumbstickMaxPercentGive = 0.97
  46. local thumbstickTouches = {}
  47.  
  48. local jumpButtonUpImage = "rbxasset://textures/ui/jumpButton.png"
  49. local jumpButtonDownImage = "rbxasset://textures/ui/jumpButtonPress.png"
  50. local jumpButtonSize = 90
  51. if isSmallScreenDevice() then
  52.         jumpButtonSize = 70
  53. end
  54. local oldJumpTouches = {}
  55. local currentJumpTouch = nil
  56.  
  57. local CameraRotateSensitivity = 0.01
  58. local CameraRotateDeadZone = CameraRotateSensitivity * 16
  59. local CameraZoomSensitivity = 0.05
  60. local PinchZoomDelay = 0.2
  61. local cameraTouch = nil
  62.  
  63.  
  64. -- make sure all of our images are good to go
  65. Game:GetService("ContentProvider"):Preload(thumbstickInnerImage)
  66. Game:GetService("ContentProvider"):Preload(thumbstickOuterImage)
  67. Game:GetService("ContentProvider"):Preload(jumpButtonUpImage)
  68. Game:GetService("ContentProvider"):Preload(jumpButtonDownImage)
  69.  
  70. ----------------------------------------------------------------------------
  71. ----------------------------------------------------------------------------
  72. -- Functions
  73.  
  74. function DistanceBetweenTwoPoints(point1, point2)
  75.     local dx = point2.x - point1.x
  76.     local dy = point2.y - point1.y
  77.     return math.sqrt( (dx*dx) + (dy*dy) )
  78. end
  79.  
  80. function transformFromCenterToTopLeft(pointToTranslate, guiObject)
  81.         return UDim2.new(0,pointToTranslate.x - guiObject.AbsoluteSize.x/2,0,pointToTranslate.y - guiObject.AbsoluteSize.y/2)
  82. end
  83.  
  84. function rotatePointAboutLocation(pointToRotate, pointToRotateAbout, radians)
  85.     local sinAnglePercent = math.sin(radians)
  86.     local cosAnglePercent = math.cos(radians)
  87.    
  88.     local transformedPoint = pointToRotate
  89.    
  90.     -- translate point back to origin:
  91.     transformedPoint = Vector2.new(transformedPoint.x - pointToRotateAbout.x, transformedPoint.y - pointToRotateAbout.y)
  92.    
  93.     -- rotate point
  94.     local xNew = transformedPoint.x * cosAnglePercent - transformedPoint.y * sinAnglePercent
  95.     local yNew = transformedPoint.x * sinAnglePercent + transformedPoint.y * cosAnglePercent
  96.    
  97.     -- translate point back:
  98.     transformedPoint = Vector2.new(xNew + pointToRotateAbout.x, yNew + pointToRotateAbout.y)
  99.  
  100.     return transformedPoint
  101. end
  102.  
  103. function dotProduct(v1,v2)
  104.         return ((v1.x*v2.x) + (v1.y*v2.y))
  105. end
  106.  
  107. function stationaryThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
  108.     local thumbstickOuterCenterPosition = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
  109.     local centerDiff = DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenterPosition)
  110.    
  111.     -- thumbstick is moving outside our region, need to cap its distance
  112.     if centerDiff > (thumbstickSize/2) then
  113.         local thumbVector = Vector2.new(touchLocation.x - thumbstickOuterCenterPosition.x,touchLocation.y - thumbstickOuterCenterPosition.y);
  114.         local normal = thumbVector.unit
  115.         if normal.x == math.nan or normal.x == math.inf then
  116.             normal = Vector2.new(0,normal.y)
  117.         end
  118.         if normal.y == math.nan or normal.y == math.inf then
  119.             normal = Vector2.new(normal.x,0)
  120.         end
  121.        
  122.         local newThumbstickInnerPosition = thumbstickOuterCenterPosition + (normal * (thumbstickSize/2))
  123.         thumbstickFrame.Position = transformFromCenterToTopLeft(newThumbstickInnerPosition, thumbstickFrame)
  124.     else
  125.         thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
  126.     end
  127.  
  128.     return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
  129. end
  130.  
  131. function followThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
  132.     local thumbstickOuterCenter = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
  133.    
  134.     -- thumbstick is moving outside our region, need to position outer thumbstick texture carefully (to make look and feel like actual joystick controller)
  135.     if DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenter) > thumbstickSize/2 then
  136.                 local thumbstickInnerCenter = Vector2.new(thumbstickFrame.Position.X.Offset + thumbstickFrame.AbsoluteSize.x/2, thumbstickFrame.Position.Y.Offset + thumbstickFrame.AbsoluteSize.y/2)
  137.                 local movementVectorUnit = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y).unit
  138.  
  139.         local outerToInnerVectorCurrent = Vector2.new(thumbstickInnerCenter.x - thumbstickOuterCenter.x, thumbstickInnerCenter.y - thumbstickOuterCenter.y)
  140.         local outerToInnerVectorCurrentUnit = outerToInnerVectorCurrent.unit
  141.         local movementVector = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y)
  142.        
  143.         -- First, find the angle between the new thumbstick movement vector,
  144.         -- and the vector between thumbstick inner and thumbstick outer.
  145.         -- We will use this to pivot thumbstick outer around thumbstick inner, gives a nice joystick feel
  146.         local crossOuterToInnerWithMovement = (outerToInnerVectorCurrentUnit.x * movementVectorUnit.y) - (outerToInnerVectorCurrentUnit.y * movementVectorUnit.x)
  147.         local angle = math.atan2(crossOuterToInnerWithMovement, dotProduct(outerToInnerVectorCurrentUnit, movementVectorUnit))
  148.         local anglePercent = angle * math.min( (movementVector.magnitude)/(outerToInnerVectorCurrent.magnitude), 1.0);
  149.        
  150.         -- If angle is significant, rotate about the inner thumbsticks current center
  151.         if math.abs(anglePercent) > 0.00001 then
  152.             local outerThumbCenter = rotatePointAboutLocation(thumbstickOuterCenter, thumbstickInnerCenter, anglePercent)
  153.             thumbstickOuter.Position = transformFromCenterToTopLeft(Vector2.new(outerThumbCenter.x,outerThumbCenter.y), thumbstickOuter)
  154.         end
  155.  
  156.         -- now just translate outer thumbstick to make sure it stays nears inner thumbstick
  157.         thumbstickOuter.Position = UDim2.new(0,thumbstickOuter.Position.X.Offset+movementVector.x,0,thumbstickOuter.Position.Y.Offset+movementVector.y)
  158.     end
  159.  
  160.         thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
  161.  
  162.     -- a bit of error checking to make sure thumbsticks stay close to eachother
  163.         thumbstickFramePosition = Vector2.new(thumbstickFrame.Position.X.Offset,thumbstickFrame.Position.Y.Offset)
  164.         thumbstickOuterPosition = Vector2.new(thumbstickOuter.Position.X.Offset,thumbstickOuter.Position.Y.Offset)
  165.     if DistanceBetweenTwoPoints(thumbstickFramePosition, thumbstickOuterPosition) > thumbstickSize/2 then
  166.         local vectorWithLength = (thumbstickOuterPosition - thumbstickFramePosition).unit * thumbstickSize/2
  167.         thumbstickOuter.Position = UDim2.new(0,thumbstickFramePosition.x + vectorWithLength.x,0,thumbstickFramePosition.y + vectorWithLength.y)
  168.     end
  169.  
  170.     return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
  171. end
  172.  
  173. function movementOutsideDeadZone(movementVector)
  174.         return ( (math.abs(movementVector.x) > ThumbstickDeadZone) or (math.abs(movementVector.y) > ThumbstickDeadZone) )
  175. end
  176.  
  177. function constructThumbstick(defaultThumbstickPos, updateFunction, stationaryThumbstick)
  178.         local thumbstickFrame = Instance.new("Frame")
  179.         thumbstickFrame.Name = "ThumbstickFrame"
  180.         thumbstickFrame.Active = true
  181.         thumbstickFrame.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
  182.         thumbstickFrame.Position = defaultThumbstickPos
  183.         thumbstickFrame.BackgroundTransparency = 1
  184.  
  185.         local outerThumbstick = Instance.new("ImageLabel")
  186.         outerThumbstick.Name = "OuterThumbstick"
  187.         outerThumbstick.Image = thumbstickOuterImage
  188.         outerThumbstick.BackgroundTransparency = 1
  189.         outerThumbstick.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
  190.         outerThumbstick.Position = defaultThumbstickPos
  191.         outerThumbstick.Parent = Game.CoreGui.RobloxGui
  192.  
  193.         local innerThumbstick = Instance.new("ImageLabel")
  194.         innerThumbstick.Name = "InnerThumbstick"
  195.         innerThumbstick.Image = thumbstickInnerImage
  196.         innerThumbstick.BackgroundTransparency = 1
  197.         innerThumbstick.Size = UDim2.new(0,thumbstickSize/2,0,thumbstickSize/2)
  198.         innerThumbstick.Position = UDim2.new(0, thumbstickFrame.Size.X.Offset/2 - thumbstickSize/4, 0, thumbstickFrame.Size.Y.Offset/2 - thumbstickSize/4)
  199.         innerThumbstick.Parent = thumbstickFrame
  200.         innerThumbstick.ZIndex = 2
  201.  
  202.         local thumbstickTouch = nil
  203.         local userInputServiceTouchMovedCon = nil
  204.         local userInputSeviceTouchEndedCon = nil
  205.  
  206.         local startInputTracking = function(inputObject)
  207.                 if thumbstickTouch then return end
  208.                 if inputObject == cameraTouch then return end
  209.                 if inputObject == currentJumpTouch then return end
  210.                 if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
  211.  
  212.                 thumbstickTouch = inputObject
  213.                 table.insert(thumbstickTouches,thumbstickTouch)
  214.  
  215.                 thumbstickFrame.Position = transformFromCenterToTopLeft(thumbstickTouch.Position,thumbstickFrame)
  216.                 outerThumbstick.Position = thumbstickFrame.Position
  217.  
  218.                 userInputServiceTouchMovedCon = userInputService.TouchMoved:connect(function(movedInput)
  219.                         if movedInput == thumbstickTouch then
  220.                                 local movementVector = nil
  221.                                 if stationaryThumbstick then
  222.                                         movementVector = stationaryThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
  223.                                 else
  224.                                         movementVector = followThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
  225.                                 end
  226.  
  227.                                 if updateFunction then
  228.                                         updateFunction(movementVector,outerThumbstick.Size.X.Offset/2)
  229.                                 end
  230.                         end
  231.                 end)
  232.                 userInputSeviceTouchEndedCon = userInputService.TouchEnded:connect(function(endedInput)
  233.                         if endedInput == thumbstickTouch then
  234.                                 if updateFunction then
  235.                                         updateFunction(Vector2.new(0,0),1)
  236.                                 end
  237.  
  238.                                 userInputSeviceTouchEndedCon:disconnect()
  239.                                 userInputServiceTouchMovedCon:disconnect()
  240.  
  241.                                 thumbstickFrame.Position = defaultThumbstickPos
  242.                                 outerThumbstick.Position = defaultThumbstickPos
  243.  
  244.                                 for i, object in pairs(thumbstickTouches) do
  245.                                         if object == thumbstickTouch then
  246.                                                 table.remove(thumbstickTouches,i)
  247.                                                 break
  248.                                         end
  249.                                 end
  250.                                 thumbstickTouch = nil
  251.                         end
  252.                 end)
  253.         end
  254.  
  255.         userInputService.Changed:connect(function(prop)
  256.                 if prop == "ModalEnabled" then
  257.                         thumbstickFrame.Visible = not userInputService.ModalEnabled
  258.                         outerThumbstick.Visible = not userInputService.ModalEnabled
  259.                 end
  260.         end)
  261.  
  262.         thumbstickFrame.InputBegan:connect(startInputTracking)
  263.         return thumbstickFrame
  264. end
  265.  
  266. function setupCharacterMovement( parentFrame )
  267.         local lastMovementVector, lastMaxMovement = nil
  268.         local moveCharacterFunc = localPlayer.MoveCharacter
  269.         local moveCharacterFunction = function ( movementVector, maxMovement )
  270.                 if localPlayer then
  271.                         if movementOutsideDeadZone(movementVector) then
  272.                                 lastMovementVector = movementVector
  273.                                 lastMaxMovement = maxMovement
  274.                                 -- sometimes rounding error will not allow us to go max speed at some
  275.                                 -- thumbstick angles, fix this with a bit of fudging near 100% throttle
  276.                                 if movementVector.magnitude/maxMovement > ThumbstickMaxPercentGive then
  277.                                         maxMovement = movementVector.magnitude - 1
  278.                                 end
  279.                                 moveCharacterFunc(localPlayer, movementVector, maxMovement)
  280.                         else
  281.                                 lastMovementVector = Vector2.new(0,0)
  282.                                 lastMaxMovement = 1
  283.                                 moveCharacterFunc(localPlayer, lastMovementVector, lastMaxMovement)
  284.                         end
  285.                 end
  286.         end
  287.  
  288.         local thumbstickPos = UDim2.new(0,thumbstickSize/2,1,-thumbstickSize*1.75)
  289.         if isSmallScreenDevice() then
  290.                 thumbstickPos = UDim2.new(0,(thumbstickSize/2) - 10,1,-thumbstickSize - 20)
  291.         end
  292.         local characterThumbstick = constructThumbstick(thumbstickPos, moveCharacterFunction, false)
  293.         characterThumbstick.Name = "CharacterThumbstick"
  294.         characterThumbstick.Parent = parentFrame
  295.  
  296.         local refreshCharacterMovement = function()
  297.                 if localPlayer and moveCharacterFunc and lastMovementVector and lastMaxMovement then
  298.                         moveCharacterFunc(localPlayer, lastMovementVector, lastMaxMovement)
  299.                 end
  300.         end
  301.         return refreshCharacterMovement
  302. end
  303.  
  304.  
  305. function setupJumpButton( parentFrame )
  306.         local jumpButton = Instance.new("ImageButton")
  307.         jumpButton.Name = "JumpButton"
  308.         jumpButton.BackgroundTransparency = 1
  309.         jumpButton.Image = jumpButtonUpImage
  310.         jumpButton.Size = UDim2.new(0,jumpButtonSize,0,jumpButtonSize)
  311.         if isSmallScreenDevice() then
  312.                 jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.25), 1, -jumpButtonSize - 20)
  313.         else
  314.                 jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.75), 1, -jumpButtonSize - 120)
  315.         end
  316.  
  317.         local playerJumpFunc = localPlayer.JumpCharacter
  318.  
  319.         local doJumpLoop = function ()
  320.                 while currentJumpTouch do
  321.                         if localPlayer then
  322.                                 playerJumpFunc(localPlayer)
  323.                         end
  324.                         wait(1/60)
  325.                 end
  326.         end
  327.  
  328.         jumpButton.InputBegan:connect(function(inputObject)
  329.                 if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
  330.                 if currentJumpTouch then return end
  331.                 if inputObject == cameraTouch then return end
  332.                 for i, touch in pairs(oldJumpTouches) do
  333.                         if touch == inputObject then
  334.                                 return
  335.                         end
  336.                 end
  337.  
  338.                 currentJumpTouch = inputObject
  339.                 jumpButton.Image = jumpButtonDownImage
  340.                 doJumpLoop()          
  341.         end)
  342.         jumpButton.InputEnded:connect(function (inputObject)
  343.                 if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
  344.                
  345.                 jumpButton.Image = jumpButtonUpImage
  346.                 if inputObject == currentJumpTouch then
  347.                         table.insert(oldJumpTouches,currentJumpTouch)
  348.                         currentJumpTouch = nil
  349.                 end
  350.         end)
  351.         userInputService.InputEnded:connect(function ( globalInputObject )
  352.                 for i, touch in pairs(oldJumpTouches) do
  353.                         if touch == globalInputObject then
  354.                                 table.remove(oldJumpTouches,i)
  355.                                 break
  356.                         end
  357.                 end
  358.         end)
  359.  
  360.         userInputService.Changed:connect(function(prop)
  361.                 if prop == "ModalEnabled" then
  362.                         jumpButton.Visible = not userInputService.ModalEnabled
  363.                 end
  364.         end)
  365.  
  366.         jumpButton.Parent = parentFrame
  367. end
  368.  
  369. function  isTouchUsedByJumpButton( touch )
  370.         if touch == currentJumpTouch then return true end
  371.         for i, touchToCompare in pairs(oldJumpTouches) do
  372.                 if touch == touchToCompare then
  373.                         return true
  374.                 end
  375.         end
  376.  
  377.         return false
  378. end
  379.  
  380. function isTouchUsedByThumbstick(touch)
  381.         for i, touchToCompare in pairs(thumbstickTouches) do
  382.                 if touch == touchToCompare then
  383.                         return true
  384.                 end
  385.         end
  386.  
  387.         return false
  388. end
  389.  
  390. function setupCameraControl(parentFrame, refreshCharacterMoveFunc)
  391.         local lastPos = nil
  392.         local hasRotatedCamera = false
  393.         local rotateCameraFunc = userInputService.RotateCamera
  394.  
  395.         local pinchTime = -1
  396.         local shouldPinch = false
  397.         local lastPinchScale = nil
  398.         local zoomCameraFunc = userInputService.ZoomCamera
  399.         local pinchTouches = {}
  400.         local pinchFrame = nil
  401.  
  402.         local resetCameraRotateState = function()
  403.                 cameraTouch = nil
  404.                 hasRotatedCamera = false
  405.                 lastPos = nil
  406.         end
  407.  
  408.         local resetPinchState = function ()
  409.                 pinchTouches = {}
  410.                 lastPinchScale = nil
  411.                 shouldPinch = false
  412.                 pinchFrame:Destroy()
  413.                 pinchFrame = nil
  414.         end
  415.  
  416.         local startPinch = function(firstTouch, secondTouch)
  417.                 -- track pinching in new frame
  418.                 if pinchFrame then pinchFrame:Destroy() end -- make sure we didn't track in any mud
  419.         pinchFrame = Instance.new("Frame")
  420.         pinchFrame.Name = "PinchFrame"
  421.         pinchFrame.BackgroundTransparency = 1
  422.         pinchFrame.Parent = parentFrame
  423.         pinchFrame.Size = UDim2.new(1,0,1,0)
  424.  
  425.         pinchFrame.InputChanged:connect(function(inputObject)
  426.                 if not shouldPinch then
  427.                         resetPinchState()
  428.                         return
  429.                 end
  430.                         resetCameraRotateState()
  431.  
  432.                         if lastPinchScale == nil then -- first pinch move, just set up scale
  433.                                 if inputObject == firstTouch then
  434.                                         lastPinchScale = (inputObject.Position - secondTouch.Position).magnitude
  435.                                         firstTouch = inputObject
  436.                                 elseif inputObject == secondTouch then
  437.                                         lastPinchScale = (inputObject.Position - firstTouch.Position).magnitude
  438.                                         secondTouch = inputObject
  439.                                 end
  440.                         else -- we are now actually pinching, do comparison to last pinch size
  441.                                 local newPinchDistance = 0
  442.                                 if inputObject == firstTouch then
  443.                                         newPinchDistance = (inputObject.Position - secondTouch.Position).magnitude
  444.                                         firstTouch = inputObject
  445.                                 elseif inputObject == secondTouch then
  446.                                         newPinchDistance = (inputObject.Position - firstTouch.Position).magnitude
  447.                                         secondTouch = inputObject
  448.                                 end
  449.                                 if newPinchDistance ~= 0 then
  450.                                         local pinchDiff = newPinchDistance - lastPinchScale
  451.                                         if pinchDiff ~= 0 then
  452.                                                 zoomCameraFunc(userInputService, (pinchDiff * CameraZoomSensitivity))
  453.                                         end
  454.                                         lastPinchScale = newPinchDistance
  455.                                 end
  456.                         end
  457.         end)
  458.         pinchFrame.InputEnded:connect(function(inputObject) -- pinch is over, destroy all
  459.                 if inputObject == firstTouch or inputObject == secondTouch then
  460.                         resetPinchState()
  461.                         end
  462.             end)
  463.         end
  464.  
  465.         local pinchGestureReceivedTouch = function(inputObject)
  466.             if #pinchTouches < 1 then
  467.                 table.insert(pinchTouches,inputObject)
  468.                 pinchTime = tick()
  469.                 shouldPinch = false
  470.             elseif #pinchTouches == 1 then
  471.                 shouldPinch = ( (tick() - pinchTime) <= PinchZoomDelay )
  472.  
  473.                 if shouldPinch then
  474.                         table.insert(pinchTouches,inputObject)
  475.                         startPinch(pinchTouches[1], pinchTouches[2])
  476.                 else -- shouldn't ever get here, but just in case
  477.                         pinchTouches = {}
  478.                 end
  479.             end
  480.         end
  481.  
  482.         parentFrame.InputBegan:connect(function (inputObject)
  483.                 if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
  484.                 if isTouchUsedByJumpButton(inputObject) then return end
  485.  
  486.                 local usedByThumbstick = isTouchUsedByThumbstick(inputObject)
  487.                 if not usedByThumbstick then
  488.                         pinchGestureReceivedTouch(inputObject)
  489.                 end
  490.  
  491.                 if cameraTouch == nil and not usedByThumbstick then
  492.                         cameraTouch = inputObject
  493.                         lastPos = Vector2.new(cameraTouch.Position.x,cameraTouch.Position.y)
  494.                         lastTick = tick()
  495.                 end
  496.         end)
  497.         userInputService.InputChanged:connect(function (inputObject)
  498.                 if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
  499.                 if cameraTouch ~= inputObject then return end
  500.  
  501.                 local newPos = Vector2.new(cameraTouch.Position.x,cameraTouch.Position.y)
  502.                 local touchDiff = (lastPos - newPos) * CameraRotateSensitivity
  503.  
  504.                 -- first time rotating outside deadzone, just setup for next changed event
  505.                 if not hasRotatedCamera and (touchDiff.magnitude > CameraRotateDeadZone) then
  506.                         hasRotatedCamera = true
  507.                         lastPos = newPos
  508.                 end
  509.  
  510.                 -- fire everytime after we have rotated out of deadzone
  511.                 if hasRotatedCamera and (lastPos ~= newPos) then
  512.                         rotateCameraFunc(userInputService, touchDiff)
  513.                         refreshCharacterMoveFunc()
  514.                         lastPos = newPos
  515.                 end
  516.         end)
  517.         userInputService.InputEnded:connect(function (inputObject)
  518.                 if cameraTouch == inputObject or cameraTouch == nil then
  519.                         resetCameraRotateState()
  520.                 end
  521.  
  522.                 for i, touch in pairs(pinchTouches) do
  523.                         if touch == inputObject then
  524.                                 table.remove(pinchTouches,i)
  525.                         end
  526.                 end
  527.         end)
  528. end
  529.  
  530. function setupTouchControls()
  531.         local touchControlFrame = Instance.new("Frame")
  532.         touchControlFrame.Name = "TouchControlFrame"
  533.         touchControlFrame.Size = UDim2.new(1,0,1,0)
  534.         touchControlFrame.BackgroundTransparency = 1
  535.         touchControlFrame.Parent = Game.CoreGui.RobloxGui
  536.  
  537.         local refreshCharacterMoveFunc = setupCharacterMovement(touchControlFrame)
  538.         setupJumpButton(touchControlFrame)
  539.         setupCameraControl(touchControlFrame, refreshCharacterMoveFunc)
  540.  
  541.         userInputService.ProcessedEvent:connect(function(inputObject, processed)
  542.                 if not processed then return end
  543.  
  544.                 -- kill camera pan if the touch is used by some user controls
  545.                 if inputObject == cameraTouch and inputObject.UserInputState == Enum.UserInputState.Begin then
  546.                         cameraTouch = nil
  547.                 end
  548.         end)
  549. end
  550.  
  551.  
  552. ----------------------------------------------------------------------------
  553. ----------------------------------------------------------------------------
  554. -- Start of Script
  555.  
  556. if userInputService:IsLuaTouchControls() then
  557.         setupTouchControls()
  558. else
  559.         script:Destroy()
  560. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement