Advertisement
DiscordPastebins

Old mobile controls on pc roblox

Jun 7th, 2024
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.97 KB | None | 0 0
  1. -- Gui to Lua
  2. -- Version: 3.2
  3.  
  4. -- Instances:
  5.  
  6. local TouchControls = Instance.new("ScreenGui")
  7.  
  8. --Properties:
  9.  
  10. TouchControls.Name = "TouchControls"
  11. TouchControls.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
  12.  
  13. -- Scripts:
  14.  
  15. local function BVZQZP_fake_script() -- TouchControls.Loader
  16. local script = Instance.new('LocalScript', TouchControls)
  17.  
  18. local userInputService = Game:GetService("UserInputService")
  19.  
  20. ----------------------------------------------------------------------------
  21. ----------------------------------------------------------------------------
  22. -- Variables
  23.  
  24. local mouse = game.Players.LocalPlayer:GetMouse()
  25. local screenResolution = Vector2.new(mouse.ViewSizeX,mouse.ViewSizeY)
  26.  
  27. function isSmallScreenDevice()
  28. return screenResolution.y <= 500
  29. end
  30.  
  31. local localPlayer = Game.Players.LocalPlayer
  32.  
  33. local isInThumbstickMode = false
  34.  
  35. local thumbstickInactiveAlpha = 0.3
  36. local thumbstickSize = 120
  37. if isSmallScreenDevice() then
  38. thumbstickSize = 70
  39. end
  40.  
  41. local touchControlsSheet = "rbxasset://textures/ui/TouchControlsSheet.png"
  42. local ThumbstickDeadZone = 5
  43. local ThumbstickMaxPercentGive = 0.92
  44. local thumbstickTouches = {}
  45.  
  46. local jumpButtonSize = 90
  47. if isSmallScreenDevice() then
  48. jumpButtonSize = 70
  49. end
  50. local oldJumpTouches = {}
  51. local currentJumpTouch = nil
  52. local lastSpeedUpdate = 0
  53. local currentSpeed = 16
  54.  
  55. Game:GetService("ContentProvider"):Preload(touchControlsSheet)
  56.  
  57. ----------------------------------------------------------------------------
  58. ----------------------------------------------------------------------------
  59. -- Functions
  60.  
  61. local isPointInRect = function(point, rectPos, rectSize)
  62. if point.x >= rectPos.x and point.x <= (rectPos.x + rectSize.x) then
  63. if point.y >= rectPos.y and point.y <= (rectPos.y + rectSize.y) then
  64. return true
  65. end
  66. end
  67.  
  68. return false
  69. end
  70.  
  71.  
  72. --
  73. -- Thumbstick Control
  74. --
  75.  
  76. function DistanceBetweenTwoPoints(point1, point2)
  77. local dx = point2.x - point1.x
  78. local dy = point2.y - point1.y
  79. return math.sqrt( (dx*dx) + (dy*dy) )
  80. end
  81.  
  82. function transformFromCenterToTopLeft(pointToTranslate, guiObject)
  83. return UDim2.new(0,pointToTranslate.x - guiObject.AbsoluteSize.x/2,0,pointToTranslate.y - guiObject.AbsoluteSize.y/2)
  84. end
  85.  
  86. function rotatePointAboutLocation(pointToRotate, pointToRotateAbout, radians)
  87. local sinAnglePercent = math.sin(radians)
  88. local cosAnglePercent = math.cos(radians)
  89.  
  90. local transformedPoint = pointToRotate
  91.  
  92. -- translate point back to origin:
  93. transformedPoint = Vector2.new(transformedPoint.x - pointToRotateAbout.x, transformedPoint.y - pointToRotateAbout.y)
  94.  
  95. -- rotate point
  96. local xNew = transformedPoint.x * cosAnglePercent - transformedPoint.y * sinAnglePercent
  97. local yNew = transformedPoint.x * sinAnglePercent + transformedPoint.y * cosAnglePercent
  98.  
  99. -- translate point back:
  100. transformedPoint = Vector2.new(xNew + pointToRotateAbout.x, yNew + pointToRotateAbout.y)
  101.  
  102. return transformedPoint
  103. end
  104.  
  105. function dotProduct(v1,v2)
  106. return ((v1.x*v2.x) + (v1.y*v2.y))
  107. end
  108.  
  109. function stationaryThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
  110. local thumbstickOuterCenterPosition = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
  111. local centerDiff = DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenterPosition)
  112.  
  113. -- thumbstick is moving outside our region, need to cap its distance
  114. if centerDiff > (thumbstickSize/2) then
  115. local thumbVector = Vector2.new(touchLocation.x - thumbstickOuterCenterPosition.x,touchLocation.y - thumbstickOuterCenterPosition.y);
  116. local normal = thumbVector.unit
  117. if normal.x == math.nan or normal.x == math.inf then
  118. normal = Vector2.new(0,normal.y)
  119. end
  120. if normal.y == math.nan or normal.y == math.inf then
  121. normal = Vector2.new(normal.x,0)
  122. end
  123.  
  124. local newThumbstickInnerPosition = thumbstickOuterCenterPosition + (normal * (thumbstickSize/2))
  125. thumbstickFrame.Position = transformFromCenterToTopLeft(newThumbstickInnerPosition, thumbstickFrame)
  126. else
  127. thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
  128. end
  129.  
  130. return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
  131. end
  132.  
  133. function followThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
  134. local thumbstickOuterCenter = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
  135.  
  136. -- thumbstick is moving outside our region, need to position outer thumbstick texture carefully (to make look and feel like actual joystick controller)
  137. if DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenter) > thumbstickSize/2 then
  138. local thumbstickInnerCenter = Vector2.new(thumbstickFrame.Position.X.Offset + thumbstickFrame.AbsoluteSize.x/2, thumbstickFrame.Position.Y.Offset + thumbstickFrame.AbsoluteSize.y/2)
  139. local movementVectorUnit = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y).unit
  140.  
  141. local outerToInnerVectorCurrent = Vector2.new(thumbstickInnerCenter.x - thumbstickOuterCenter.x, thumbstickInnerCenter.y - thumbstickOuterCenter.y)
  142. local outerToInnerVectorCurrentUnit = outerToInnerVectorCurrent.unit
  143. local movementVector = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y)
  144.  
  145. -- First, find the angle between the new thumbstick movement vector,
  146. -- and the vector between thumbstick inner and thumbstick outer.
  147. -- We will use this to pivot thumbstick outer around thumbstick inner, gives a nice joystick feel
  148. local crossOuterToInnerWithMovement = (outerToInnerVectorCurrentUnit.x * movementVectorUnit.y) - (outerToInnerVectorCurrentUnit.y * movementVectorUnit.x)
  149. local angle = math.atan2(crossOuterToInnerWithMovement, dotProduct(outerToInnerVectorCurrentUnit, movementVectorUnit))
  150. local anglePercent = angle * math.min( (movementVector.magnitude)/(outerToInnerVectorCurrent.magnitude), 1.0);
  151.  
  152. -- If angle is significant, rotate about the inner thumbsticks current center
  153. if math.abs(anglePercent) > 0.00001 then
  154. local outerThumbCenter = rotatePointAboutLocation(thumbstickOuterCenter, thumbstickInnerCenter, anglePercent)
  155. thumbstickOuter.Position = transformFromCenterToTopLeft(Vector2.new(outerThumbCenter.x,outerThumbCenter.y), thumbstickOuter)
  156. end
  157.  
  158. -- now just translate outer thumbstick to make sure it stays nears inner thumbstick
  159. thumbstickOuter.Position = UDim2.new(0,thumbstickOuter.Position.X.Offset+movementVector.x,0,thumbstickOuter.Position.Y.Offset+movementVector.y)
  160. end
  161.  
  162. thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
  163.  
  164. -- a bit of error checking to make sure thumbsticks stay close to eachother
  165. thumbstickFramePosition = Vector2.new(thumbstickFrame.Position.X.Offset,thumbstickFrame.Position.Y.Offset)
  166. thumbstickOuterPosition = Vector2.new(thumbstickOuter.Position.X.Offset,thumbstickOuter.Position.Y.Offset)
  167. if DistanceBetweenTwoPoints(thumbstickFramePosition, thumbstickOuterPosition) > thumbstickSize/2 then
  168. local vectorWithLength = (thumbstickOuterPosition - thumbstickFramePosition).unit * thumbstickSize/2
  169. thumbstickOuter.Position = UDim2.new(0,thumbstickFramePosition.x + vectorWithLength.x,0,thumbstickFramePosition.y + vectorWithLength.y)
  170. end
  171.  
  172. return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
  173. end
  174.  
  175. function movementOutsideDeadZone(movementVector)
  176. return ( (math.abs(movementVector.x) > ThumbstickDeadZone) or (math.abs(movementVector.y) > ThumbstickDeadZone) )
  177. end
  178.  
  179. function constructThumbstick(defaultThumbstickPos, updateFunction, stationaryThumbstick)
  180. local thumbstickFrame = Instance.new("Frame")
  181. thumbstickFrame.Name = "ThumbstickFrame"
  182. thumbstickFrame.Active = true
  183. thumbstickFrame.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
  184. thumbstickFrame.Position = defaultThumbstickPos
  185. thumbstickFrame.BackgroundTransparency = 1
  186.  
  187. local outerThumbstick = Instance.new("ImageLabel")
  188. outerThumbstick.Name = "OuterThumbstick"
  189. outerThumbstick.Image = touchControlsSheet
  190. outerThumbstick.ImageRectOffset = Vector2.new(0,0)
  191. outerThumbstick.ImageRectSize = Vector2.new(220,220)
  192. outerThumbstick.BackgroundTransparency = 1
  193. outerThumbstick.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
  194. outerThumbstick.Position = defaultThumbstickPos
  195. outerThumbstick.Parent = script.Parent
  196.  
  197. local innerThumbstick = Instance.new("ImageLabel")
  198. innerThumbstick.Name = "InnerThumbstick"
  199. innerThumbstick.Image = touchControlsSheet
  200. innerThumbstick.ImageRectOffset = Vector2.new(220,0)
  201. innerThumbstick.ImageRectSize = Vector2.new(111,111)
  202. innerThumbstick.BackgroundTransparency = 1
  203. innerThumbstick.Size = UDim2.new(0,thumbstickSize/2,0,thumbstickSize/2)
  204. innerThumbstick.Position = UDim2.new(0, thumbstickFrame.Size.X.Offset/2 - thumbstickSize/4, 0, thumbstickFrame.Size.Y.Offset/2 - thumbstickSize/4)
  205. innerThumbstick.Parent = thumbstickFrame
  206. innerThumbstick.ZIndex = 2
  207.  
  208. local thumbstickTouch
  209. local userInputServiceMoveCon
  210. local userInputServiceMouseEnd
  211. local humanoidCheck
  212. local char
  213.  
  214. local startInputTracking = function(inputObject)
  215. if thumbstickTouch then return end
  216. if inputObject == currentJumpTouch then return end
  217. if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
  218.  
  219. thumbstickTouch = inputObject
  220. table.insert(thumbstickTouches,thumbstickTouch)
  221.  
  222. char = game.Players.LocalPlayer.Character
  223. if char then
  224. if char:findFirstChild("Humanoid") then
  225. currentSpeed = char.Humanoid.WalkSpeed
  226. humanoidCheck = char.Humanoid.Changed:connect(function (property)
  227. if property == "WalkSpeed" then
  228. pcall(function ()
  229. -- Sometimes this errors, but it doesn't break the code. Thats why I put it into a pcall.
  230. if string.sub(tostring(char.Humanoid.WalkSpeed,1,4)) ~= string.sub(tostring(lastSpeedUpdate),1,4) then -- Floats are annoying.
  231. currentSpeed = char.Humanoid.WalkSpeed
  232. else
  233. char.Humanoid.WalkSpeed = lastSpeedUpdate
  234. end
  235. end)
  236. end
  237. end)
  238. end
  239. end
  240. thumbstickFrame.Position = transformFromCenterToTopLeft(thumbstickTouch.Position,thumbstickFrame)
  241. outerThumbstick.Position = thumbstickFrame.Position
  242.  
  243. local function handleMovement(movedInput)
  244. local movementVector = nil
  245. if stationaryThumbstick then
  246. movementVector = stationaryThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
  247. else
  248. movementVector = followThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
  249. end
  250.  
  251. if updateFunction then
  252. updateFunction(movementVector,outerThumbstick.Size.X.Offset/2)
  253. end
  254. end
  255. local function handleMovementEnd(endedInput)
  256. if updateFunction then
  257. updateFunction(Vector2.new(0,0),1)
  258. end
  259.  
  260. userInputServiceMoveCon:disconnect()
  261. userInputServiceMouseEnd:disconnect()
  262. humanoidCheck:disconnect()
  263.  
  264. if char and char:findFirstChild("Humanoid") then
  265. char.Humanoid.WalkSpeed = currentSpeed
  266. end
  267.  
  268. thumbstickFrame.Position = defaultThumbstickPos
  269. outerThumbstick.Position = defaultThumbstickPos
  270.  
  271. for i, object in pairs(thumbstickTouches) do
  272. if object == thumbstickTouch then
  273. table.remove(thumbstickTouches,i)
  274. break
  275. end
  276. end
  277. thumbstickTouch = nil
  278. end
  279. lastPos = Vector2.new(0,0)
  280. userInputServiceMoveCon = userInputService.InputChanged:connect(function (movedInput)
  281. local x,y = movedInput.Position.X,movedInput.Position.Y
  282. local delta = Vector2.new(x,y)-lastPos
  283. if delta.X ~= 0 or delta.Y ~= 0 then
  284. lastPos = Vector2.new(x,y)
  285. if movedInput.UserInputType == Enum.UserInputType.MouseMovement then
  286. handleMovement(movedInput)
  287. end
  288. end
  289. end)
  290. userInputServiceMouseEnd = userInputService.InputEnded:connect(function (endInput)
  291. if endInput.UserInputType == Enum.UserInputType.MouseButton1 then
  292. handleMovementEnd(endedInput)
  293. end
  294. end)
  295. end
  296.  
  297. thumbstickFrame.InputBegan:connect(startInputTracking)
  298. return thumbstickFrame, outerThumbstick
  299. end
  300.  
  301. ----------------------------------------------------------------------------
  302. ----------------------------------------------------------------------------
  303. --
  304. -- Character Movement
  305. --
  306. local characterThumbstick = nil
  307. local characterOuterThumbstick = nil
  308.  
  309. local function moveCharacter(m,r)
  310. -- Behavioral remake of Player:MoveCharacter(m,r)
  311. -- (Its locked :P)
  312. local char = game.Players.LocalPlayer.Character
  313. if char then
  314. local h = char:findFirstChild("Humanoid")
  315. if h then
  316. local torso = h.Torso
  317. local cf = CFrame.new(torso.Position)
  318. local c = workspace.CurrentCamera
  319. local l = c.CoordinateFrame.lookVector
  320. h:MoveTo((cf * CFrame.Angles(0,-math.atan2(l.Z,l.X)-math.pi/2,0) * CFrame.new(m.X*100,0,m.Y*100)).p)
  321. local speed = math.min(currentSpeed,currentSpeed * math.sqrt(m.X^2 + m.Y^2)/r)
  322. if speed ~= 0 then
  323. lastSpeedUpdate = speed
  324. h.WalkSpeed = speed
  325. end
  326. end
  327. end
  328. end
  329.  
  330. function setupCharacterMovement( parentFrame )
  331. local lastMovementVector, lastMaxMovement = nil
  332. local moveCharacterFunction = function ( movementVector, maxMovement )
  333. if localPlayer then
  334. if movementOutsideDeadZone(movementVector) then
  335. lastMovementVector = movementVector
  336. lastMaxMovement = maxMovement
  337. -- sometimes rounding error will not allow us to go max speed at some
  338. -- thumbstick angles, fix this with a bit of fudging near 100% throttle
  339. if movementVector.magnitude/maxMovement > ThumbstickMaxPercentGive then
  340. maxMovement = movementVector.magnitude - 1
  341. end
  342. moveCharacter(movementVector, maxMovement)
  343. else
  344. lastMovementVector = Vector2.new(0,0)
  345. lastMaxMovement = 1
  346. moveCharacter(lastMovementVector, lastMaxMovement)
  347. end
  348. end
  349. end
  350. local thumbstickPos = UDim2.new(0,thumbstickSize/2,1,-thumbstickSize*1.75)
  351. if isSmallScreenDevice() then
  352. thumbstickPos = UDim2.new(0,(thumbstickSize/2) - 10,1,-thumbstickSize - 20)
  353. end
  354. characterThumbstick, characterOuterThumbstick = constructThumbstick(thumbstickPos, moveCharacterFunction, false)
  355. characterThumbstick.Name = "CharacterThumbstick"
  356. characterThumbstick.Parent = parentFrame
  357. end
  358.  
  359. ----------------------------------------------------------------------------
  360. ----------------------------------------------------------------------------
  361. --
  362. -- Jump Button
  363. --
  364. local jumpButton = nil
  365.  
  366. function setupJumpButton( parentFrame )
  367. if (jumpButton == nil) then
  368. jumpButton = Instance.new("ImageButton")
  369. jumpButton.Name = "JumpButton"
  370. jumpButton.BackgroundTransparency = 1
  371. jumpButton.Image = touchControlsSheet
  372. jumpButton.ImageRectOffset = Vector2.new(176,222)
  373. jumpButton.ImageRectSize = Vector2.new(174,174)
  374. jumpButton.Size = UDim2.new(0,jumpButtonSize,0,jumpButtonSize)
  375. if isSmallScreenDevice() then
  376. jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.25), 1, -jumpButtonSize - 20)
  377. else
  378. jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.75), 1, -jumpButtonSize - 120)
  379. end
  380.  
  381. local doJumpLoop = function ()
  382. while currentJumpTouch do
  383. if localPlayer then
  384. if localPlayer.Character then
  385. if localPlayer.Character:findFirstChild("Humanoid") then
  386. localPlayer.Character.Humanoid.Jump = true
  387. end
  388. end
  389. end
  390. wait(1/60)
  391. end
  392. end
  393.  
  394. jumpButton.InputBegan:connect(function(inputObject)
  395. if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
  396. if currentJumpTouch then return end
  397. for i, touch in pairs(oldJumpTouches) do
  398. if touch == inputObject then
  399. return
  400. end
  401. end
  402.  
  403. currentJumpTouch = inputObject
  404. jumpButton.ImageRectOffset = Vector2.new(0,222)
  405. jumpButton.ImageRectSize = Vector2.new(174,174)
  406. doJumpLoop()
  407. end)
  408. jumpButton.InputEnded:connect(function (inputObject)
  409. if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
  410.  
  411. jumpButton.ImageRectOffset = Vector2.new(176,222)
  412. jumpButton.ImageRectSize = Vector2.new(174,174)
  413.  
  414. if inputObject == currentJumpTouch then
  415. table.insert(oldJumpTouches,currentJumpTouch)
  416. currentJumpTouch = nil
  417. end
  418. end)
  419. userInputService.InputEnded:connect(function ( globalInputObject )
  420. for i, touch in pairs(oldJumpTouches) do
  421. if touch == globalInputObject then
  422. table.remove(oldJumpTouches,i)
  423. break
  424. end
  425. end
  426. end)
  427. jumpButton.Parent = parentFrame
  428. end
  429. jumpButton.Visible = not userInputService.ModalEnabled
  430. end
  431.  
  432. function isTouchUsedByJumpButton( touch )
  433. if touch == currentJumpTouch then return true end
  434. for i, touchToCompare in pairs(oldJumpTouches) do
  435. if touch == touchToCompare then
  436. return true
  437. end
  438. end
  439.  
  440. return false
  441. end
  442.  
  443. function isTouchUsedByThumbstick(touch)
  444. for i, touchToCompare in pairs(thumbstickTouches) do
  445. if touch == touchToCompare then
  446. return true
  447. end
  448. end
  449.  
  450. return false
  451. end
  452.  
  453. ----------------------------------------------------------------------------
  454. ----------------------------------------------------------------------------
  455. --
  456. -- Touch Control
  457. --
  458.  
  459. local touchControlFrame = nil
  460.  
  461. function setupTouchControls()
  462. if game:GetService("UserInputService").TouchEnabled then return end
  463. Spawn(function ()
  464. -- Horrific hack to disable the default roblox controls.
  465. local controller
  466. while not controller do
  467. controller = game:GetService("ControllerService")
  468. wait(.25)
  469. end
  470. if controller:GetChildren()[1] then
  471. controller:GetChildren()[1]:Destroy()
  472. end
  473. controller.ChildAdded:connect(function (child)
  474. wait()
  475. child:Destroy()
  476. end)
  477. end)
  478.  
  479. wait(2) -- Wait a minute so the resolution can get set :P
  480.  
  481. touchControlFrame = Instance.new("Frame")
  482. touchControlFrame.Name = "TouchControlFrame"
  483. touchControlFrame.Size = UDim2.new(1,0,1,0)
  484. touchControlFrame.BackgroundTransparency = 1
  485. touchControlFrame.Parent = script.Parent
  486.  
  487. setupJumpButton(touchControlFrame)
  488. setupCharacterMovement(touchControlFrame)
  489.  
  490. end
  491.  
  492. setupTouchControls()
  493. end
  494. coroutine.wrap(BVZQZP_fake_script)()
  495.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement