Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Gui to Lua
- -- Version: 3.2
- -- Instances:
- local TouchControls = Instance.new("ScreenGui")
- --Properties:
- TouchControls.Name = "TouchControls"
- TouchControls.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
- -- Scripts:
- local function BVZQZP_fake_script() -- TouchControls.Loader
- local script = Instance.new('LocalScript', TouchControls)
- local userInputService = Game:GetService("UserInputService")
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- -- Variables
- local mouse = game.Players.LocalPlayer:GetMouse()
- local screenResolution = Vector2.new(mouse.ViewSizeX,mouse.ViewSizeY)
- function isSmallScreenDevice()
- return screenResolution.y <= 500
- end
- local localPlayer = Game.Players.LocalPlayer
- local isInThumbstickMode = false
- local thumbstickInactiveAlpha = 0.3
- local thumbstickSize = 120
- if isSmallScreenDevice() then
- thumbstickSize = 70
- end
- local touchControlsSheet = "rbxasset://textures/ui/TouchControlsSheet.png"
- local ThumbstickDeadZone = 5
- local ThumbstickMaxPercentGive = 0.92
- local thumbstickTouches = {}
- local jumpButtonSize = 90
- if isSmallScreenDevice() then
- jumpButtonSize = 70
- end
- local oldJumpTouches = {}
- local currentJumpTouch = nil
- local lastSpeedUpdate = 0
- local currentSpeed = 16
- Game:GetService("ContentProvider"):Preload(touchControlsSheet)
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- -- Functions
- local isPointInRect = function(point, rectPos, rectSize)
- if point.x >= rectPos.x and point.x <= (rectPos.x + rectSize.x) then
- if point.y >= rectPos.y and point.y <= (rectPos.y + rectSize.y) then
- return true
- end
- end
- return false
- end
- --
- -- Thumbstick Control
- --
- function DistanceBetweenTwoPoints(point1, point2)
- local dx = point2.x - point1.x
- local dy = point2.y - point1.y
- return math.sqrt( (dx*dx) + (dy*dy) )
- end
- function transformFromCenterToTopLeft(pointToTranslate, guiObject)
- return UDim2.new(0,pointToTranslate.x - guiObject.AbsoluteSize.x/2,0,pointToTranslate.y - guiObject.AbsoluteSize.y/2)
- end
- function rotatePointAboutLocation(pointToRotate, pointToRotateAbout, radians)
- local sinAnglePercent = math.sin(radians)
- local cosAnglePercent = math.cos(radians)
- local transformedPoint = pointToRotate
- -- translate point back to origin:
- transformedPoint = Vector2.new(transformedPoint.x - pointToRotateAbout.x, transformedPoint.y - pointToRotateAbout.y)
- -- rotate point
- local xNew = transformedPoint.x * cosAnglePercent - transformedPoint.y * sinAnglePercent
- local yNew = transformedPoint.x * sinAnglePercent + transformedPoint.y * cosAnglePercent
- -- translate point back:
- transformedPoint = Vector2.new(xNew + pointToRotateAbout.x, yNew + pointToRotateAbout.y)
- return transformedPoint
- end
- function dotProduct(v1,v2)
- return ((v1.x*v2.x) + (v1.y*v2.y))
- end
- function stationaryThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
- local thumbstickOuterCenterPosition = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
- local centerDiff = DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenterPosition)
- -- thumbstick is moving outside our region, need to cap its distance
- if centerDiff > (thumbstickSize/2) then
- local thumbVector = Vector2.new(touchLocation.x - thumbstickOuterCenterPosition.x,touchLocation.y - thumbstickOuterCenterPosition.y);
- local normal = thumbVector.unit
- if normal.x == math.nan or normal.x == math.inf then
- normal = Vector2.new(0,normal.y)
- end
- if normal.y == math.nan or normal.y == math.inf then
- normal = Vector2.new(normal.x,0)
- end
- local newThumbstickInnerPosition = thumbstickOuterCenterPosition + (normal * (thumbstickSize/2))
- thumbstickFrame.Position = transformFromCenterToTopLeft(newThumbstickInnerPosition, thumbstickFrame)
- else
- thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
- end
- return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
- end
- function followThumbstickTouchMove(thumbstickFrame, thumbstickOuter, touchLocation)
- local thumbstickOuterCenter = Vector2.new(thumbstickOuter.Position.X.Offset + thumbstickOuter.AbsoluteSize.x/2, thumbstickOuter.Position.Y.Offset + thumbstickOuter.AbsoluteSize.y/2)
- -- thumbstick is moving outside our region, need to position outer thumbstick texture carefully (to make look and feel like actual joystick controller)
- if DistanceBetweenTwoPoints(touchLocation, thumbstickOuterCenter) > thumbstickSize/2 then
- local thumbstickInnerCenter = Vector2.new(thumbstickFrame.Position.X.Offset + thumbstickFrame.AbsoluteSize.x/2, thumbstickFrame.Position.Y.Offset + thumbstickFrame.AbsoluteSize.y/2)
- local movementVectorUnit = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y).unit
- local outerToInnerVectorCurrent = Vector2.new(thumbstickInnerCenter.x - thumbstickOuterCenter.x, thumbstickInnerCenter.y - thumbstickOuterCenter.y)
- local outerToInnerVectorCurrentUnit = outerToInnerVectorCurrent.unit
- local movementVector = Vector2.new(touchLocation.x - thumbstickInnerCenter.x, touchLocation.y - thumbstickInnerCenter.y)
- -- First, find the angle between the new thumbstick movement vector,
- -- and the vector between thumbstick inner and thumbstick outer.
- -- We will use this to pivot thumbstick outer around thumbstick inner, gives a nice joystick feel
- local crossOuterToInnerWithMovement = (outerToInnerVectorCurrentUnit.x * movementVectorUnit.y) - (outerToInnerVectorCurrentUnit.y * movementVectorUnit.x)
- local angle = math.atan2(crossOuterToInnerWithMovement, dotProduct(outerToInnerVectorCurrentUnit, movementVectorUnit))
- local anglePercent = angle * math.min( (movementVector.magnitude)/(outerToInnerVectorCurrent.magnitude), 1.0);
- -- If angle is significant, rotate about the inner thumbsticks current center
- if math.abs(anglePercent) > 0.00001 then
- local outerThumbCenter = rotatePointAboutLocation(thumbstickOuterCenter, thumbstickInnerCenter, anglePercent)
- thumbstickOuter.Position = transformFromCenterToTopLeft(Vector2.new(outerThumbCenter.x,outerThumbCenter.y), thumbstickOuter)
- end
- -- now just translate outer thumbstick to make sure it stays nears inner thumbstick
- thumbstickOuter.Position = UDim2.new(0,thumbstickOuter.Position.X.Offset+movementVector.x,0,thumbstickOuter.Position.Y.Offset+movementVector.y)
- end
- thumbstickFrame.Position = transformFromCenterToTopLeft(touchLocation,thumbstickFrame)
- -- a bit of error checking to make sure thumbsticks stay close to eachother
- thumbstickFramePosition = Vector2.new(thumbstickFrame.Position.X.Offset,thumbstickFrame.Position.Y.Offset)
- thumbstickOuterPosition = Vector2.new(thumbstickOuter.Position.X.Offset,thumbstickOuter.Position.Y.Offset)
- if DistanceBetweenTwoPoints(thumbstickFramePosition, thumbstickOuterPosition) > thumbstickSize/2 then
- local vectorWithLength = (thumbstickOuterPosition - thumbstickFramePosition).unit * thumbstickSize/2
- thumbstickOuter.Position = UDim2.new(0,thumbstickFramePosition.x + vectorWithLength.x,0,thumbstickFramePosition.y + vectorWithLength.y)
- end
- return Vector2.new(thumbstickFrame.Position.X.Offset - thumbstickOuter.Position.X.Offset,thumbstickFrame.Position.Y.Offset - thumbstickOuter.Position.Y.Offset)
- end
- function movementOutsideDeadZone(movementVector)
- return ( (math.abs(movementVector.x) > ThumbstickDeadZone) or (math.abs(movementVector.y) > ThumbstickDeadZone) )
- end
- function constructThumbstick(defaultThumbstickPos, updateFunction, stationaryThumbstick)
- local thumbstickFrame = Instance.new("Frame")
- thumbstickFrame.Name = "ThumbstickFrame"
- thumbstickFrame.Active = true
- thumbstickFrame.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
- thumbstickFrame.Position = defaultThumbstickPos
- thumbstickFrame.BackgroundTransparency = 1
- local outerThumbstick = Instance.new("ImageLabel")
- outerThumbstick.Name = "OuterThumbstick"
- outerThumbstick.Image = touchControlsSheet
- outerThumbstick.ImageRectOffset = Vector2.new(0,0)
- outerThumbstick.ImageRectSize = Vector2.new(220,220)
- outerThumbstick.BackgroundTransparency = 1
- outerThumbstick.Size = UDim2.new(0,thumbstickSize,0,thumbstickSize)
- outerThumbstick.Position = defaultThumbstickPos
- outerThumbstick.Parent = script.Parent
- local innerThumbstick = Instance.new("ImageLabel")
- innerThumbstick.Name = "InnerThumbstick"
- innerThumbstick.Image = touchControlsSheet
- innerThumbstick.ImageRectOffset = Vector2.new(220,0)
- innerThumbstick.ImageRectSize = Vector2.new(111,111)
- innerThumbstick.BackgroundTransparency = 1
- innerThumbstick.Size = UDim2.new(0,thumbstickSize/2,0,thumbstickSize/2)
- innerThumbstick.Position = UDim2.new(0, thumbstickFrame.Size.X.Offset/2 - thumbstickSize/4, 0, thumbstickFrame.Size.Y.Offset/2 - thumbstickSize/4)
- innerThumbstick.Parent = thumbstickFrame
- innerThumbstick.ZIndex = 2
- local thumbstickTouch
- local userInputServiceMoveCon
- local userInputServiceMouseEnd
- local humanoidCheck
- local char
- local startInputTracking = function(inputObject)
- if thumbstickTouch then return end
- if inputObject == currentJumpTouch then return end
- if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
- thumbstickTouch = inputObject
- table.insert(thumbstickTouches,thumbstickTouch)
- char = game.Players.LocalPlayer.Character
- if char then
- if char:findFirstChild("Humanoid") then
- currentSpeed = char.Humanoid.WalkSpeed
- humanoidCheck = char.Humanoid.Changed:connect(function (property)
- if property == "WalkSpeed" then
- pcall(function ()
- -- Sometimes this errors, but it doesn't break the code. Thats why I put it into a pcall.
- if string.sub(tostring(char.Humanoid.WalkSpeed,1,4)) ~= string.sub(tostring(lastSpeedUpdate),1,4) then -- Floats are annoying.
- currentSpeed = char.Humanoid.WalkSpeed
- else
- char.Humanoid.WalkSpeed = lastSpeedUpdate
- end
- end)
- end
- end)
- end
- end
- thumbstickFrame.Position = transformFromCenterToTopLeft(thumbstickTouch.Position,thumbstickFrame)
- outerThumbstick.Position = thumbstickFrame.Position
- local function handleMovement(movedInput)
- local movementVector = nil
- if stationaryThumbstick then
- movementVector = stationaryThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
- else
- movementVector = followThumbstickTouchMove(thumbstickFrame,outerThumbstick,Vector2.new(movedInput.Position.x,movedInput.Position.y))
- end
- if updateFunction then
- updateFunction(movementVector,outerThumbstick.Size.X.Offset/2)
- end
- end
- local function handleMovementEnd(endedInput)
- if updateFunction then
- updateFunction(Vector2.new(0,0),1)
- end
- userInputServiceMoveCon:disconnect()
- userInputServiceMouseEnd:disconnect()
- humanoidCheck:disconnect()
- if char and char:findFirstChild("Humanoid") then
- char.Humanoid.WalkSpeed = currentSpeed
- end
- thumbstickFrame.Position = defaultThumbstickPos
- outerThumbstick.Position = defaultThumbstickPos
- for i, object in pairs(thumbstickTouches) do
- if object == thumbstickTouch then
- table.remove(thumbstickTouches,i)
- break
- end
- end
- thumbstickTouch = nil
- end
- lastPos = Vector2.new(0,0)
- userInputServiceMoveCon = userInputService.InputChanged:connect(function (movedInput)
- local x,y = movedInput.Position.X,movedInput.Position.Y
- local delta = Vector2.new(x,y)-lastPos
- if delta.X ~= 0 or delta.Y ~= 0 then
- lastPos = Vector2.new(x,y)
- if movedInput.UserInputType == Enum.UserInputType.MouseMovement then
- handleMovement(movedInput)
- end
- end
- end)
- userInputServiceMouseEnd = userInputService.InputEnded:connect(function (endInput)
- if endInput.UserInputType == Enum.UserInputType.MouseButton1 then
- handleMovementEnd(endedInput)
- end
- end)
- end
- thumbstickFrame.InputBegan:connect(startInputTracking)
- return thumbstickFrame, outerThumbstick
- end
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- --
- -- Character Movement
- --
- local characterThumbstick = nil
- local characterOuterThumbstick = nil
- local function moveCharacter(m,r)
- -- Behavioral remake of Player:MoveCharacter(m,r)
- -- (Its locked :P)
- local char = game.Players.LocalPlayer.Character
- if char then
- local h = char:findFirstChild("Humanoid")
- if h then
- local torso = h.Torso
- local cf = CFrame.new(torso.Position)
- local c = workspace.CurrentCamera
- local l = c.CoordinateFrame.lookVector
- 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)
- local speed = math.min(currentSpeed,currentSpeed * math.sqrt(m.X^2 + m.Y^2)/r)
- if speed ~= 0 then
- lastSpeedUpdate = speed
- h.WalkSpeed = speed
- end
- end
- end
- end
- function setupCharacterMovement( parentFrame )
- local lastMovementVector, lastMaxMovement = nil
- local moveCharacterFunction = function ( movementVector, maxMovement )
- if localPlayer then
- if movementOutsideDeadZone(movementVector) then
- lastMovementVector = movementVector
- lastMaxMovement = maxMovement
- -- sometimes rounding error will not allow us to go max speed at some
- -- thumbstick angles, fix this with a bit of fudging near 100% throttle
- if movementVector.magnitude/maxMovement > ThumbstickMaxPercentGive then
- maxMovement = movementVector.magnitude - 1
- end
- moveCharacter(movementVector, maxMovement)
- else
- lastMovementVector = Vector2.new(0,0)
- lastMaxMovement = 1
- moveCharacter(lastMovementVector, lastMaxMovement)
- end
- end
- end
- local thumbstickPos = UDim2.new(0,thumbstickSize/2,1,-thumbstickSize*1.75)
- if isSmallScreenDevice() then
- thumbstickPos = UDim2.new(0,(thumbstickSize/2) - 10,1,-thumbstickSize - 20)
- end
- characterThumbstick, characterOuterThumbstick = constructThumbstick(thumbstickPos, moveCharacterFunction, false)
- characterThumbstick.Name = "CharacterThumbstick"
- characterThumbstick.Parent = parentFrame
- end
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- --
- -- Jump Button
- --
- local jumpButton = nil
- function setupJumpButton( parentFrame )
- if (jumpButton == nil) then
- jumpButton = Instance.new("ImageButton")
- jumpButton.Name = "JumpButton"
- jumpButton.BackgroundTransparency = 1
- jumpButton.Image = touchControlsSheet
- jumpButton.ImageRectOffset = Vector2.new(176,222)
- jumpButton.ImageRectSize = Vector2.new(174,174)
- jumpButton.Size = UDim2.new(0,jumpButtonSize,0,jumpButtonSize)
- if isSmallScreenDevice() then
- jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.25), 1, -jumpButtonSize - 20)
- else
- jumpButton.Position = UDim2.new(1, -(jumpButtonSize*2.75), 1, -jumpButtonSize - 120)
- end
- local doJumpLoop = function ()
- while currentJumpTouch do
- if localPlayer then
- if localPlayer.Character then
- if localPlayer.Character:findFirstChild("Humanoid") then
- localPlayer.Character.Humanoid.Jump = true
- end
- end
- end
- wait(1/60)
- end
- end
- jumpButton.InputBegan:connect(function(inputObject)
- if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
- if currentJumpTouch then return end
- for i, touch in pairs(oldJumpTouches) do
- if touch == inputObject then
- return
- end
- end
- currentJumpTouch = inputObject
- jumpButton.ImageRectOffset = Vector2.new(0,222)
- jumpButton.ImageRectSize = Vector2.new(174,174)
- doJumpLoop()
- end)
- jumpButton.InputEnded:connect(function (inputObject)
- if inputObject.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
- jumpButton.ImageRectOffset = Vector2.new(176,222)
- jumpButton.ImageRectSize = Vector2.new(174,174)
- if inputObject == currentJumpTouch then
- table.insert(oldJumpTouches,currentJumpTouch)
- currentJumpTouch = nil
- end
- end)
- userInputService.InputEnded:connect(function ( globalInputObject )
- for i, touch in pairs(oldJumpTouches) do
- if touch == globalInputObject then
- table.remove(oldJumpTouches,i)
- break
- end
- end
- end)
- jumpButton.Parent = parentFrame
- end
- jumpButton.Visible = not userInputService.ModalEnabled
- end
- function isTouchUsedByJumpButton( touch )
- if touch == currentJumpTouch then return true end
- for i, touchToCompare in pairs(oldJumpTouches) do
- if touch == touchToCompare then
- return true
- end
- end
- return false
- end
- function isTouchUsedByThumbstick(touch)
- for i, touchToCompare in pairs(thumbstickTouches) do
- if touch == touchToCompare then
- return true
- end
- end
- return false
- end
- ----------------------------------------------------------------------------
- ----------------------------------------------------------------------------
- --
- -- Touch Control
- --
- local touchControlFrame = nil
- function setupTouchControls()
- if game:GetService("UserInputService").TouchEnabled then return end
- Spawn(function ()
- -- Horrific hack to disable the default roblox controls.
- local controller
- while not controller do
- controller = game:GetService("ControllerService")
- wait(.25)
- end
- if controller:GetChildren()[1] then
- controller:GetChildren()[1]:Destroy()
- end
- controller.ChildAdded:connect(function (child)
- wait()
- child:Destroy()
- end)
- end)
- wait(2) -- Wait a minute so the resolution can get set :P
- touchControlFrame = Instance.new("Frame")
- touchControlFrame.Name = "TouchControlFrame"
- touchControlFrame.Size = UDim2.new(1,0,1,0)
- touchControlFrame.BackgroundTransparency = 1
- touchControlFrame.Parent = script.Parent
- setupJumpButton(touchControlFrame)
- setupCharacterMovement(touchControlFrame)
- end
- setupTouchControls()
- end
- coroutine.wrap(BVZQZP_fake_script)()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement