I have this code for a plane script on mobile that hasn’t been working right. The plane on mobile won’t respond to the user’s direction change commands via their movement on the touch screen.
I’m trying to get the plane to move based on the touch location of the mobile user (i.e. where they tap/drag/whatever), such as if the player is dragging/touching right, it’ll bank/move right, same for up, down, left, etc. This script does not respond to any of the mobile user’s touch commands, and I have no idea why. It has a similar issue for console as well.
Basically, I need to find and detect the mobile (and console) equivalent of mouse.Hit, mouse.ViewSizeX, and mouse.X on PC. The script works fine on desktop/PC, and I attribute the issue to the mouse properties I’m attempting to use, but I don’t know much about how to manipulate any input properties besides desktop.
I’m pretty new to working with mobile commands, so I’m not even sure what I have is the right approach.
Here’s my script:
---Client sided code; this is in one script
local dY --- different approach I tried
local dX
local lastTouchTranslation = nil
local function setup_Touch(touchPositions, totalTranslation, velocity, state)
local difference
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
difference = totalTranslation - lastTouchTranslation
end
lastTouchTranslation = totalTranslation
print(difference.X)
print(difference.Y) ---print statements won't work
dX = difference.X
dY = difference.Y
return difference.X, difference.Y ---I tried to get the code to work via a return of this function originally, to no avail, so I tried a different method with the dX and dY functions.
end
----below function detects mobile movement. Not sure if this is the right one to use.
UserInputService.TouchPan:Connect(function() rem.onMouseMoved:FireServer(mouse.Hit,setup_Touch) end) --- this code is in a bigger function group that detects input on different devices
---Server sided code; code works on PC perfectly with mouse input, doesn't work so much with mobile and console input. In separate script from client code.
function GetBankAngle(M,ViewSizeX,X) ---- this manages the banking features of plane, makes movement look less clunky.
local VSX,X = ViewSizeX,X
local Ratio = (((VSX / 2) - X)/(VSX / 2))
Ratio = (Ratio < -1 and -1 or Ratio > 1 and 1 or Ratio)
return math.rad(Ratio * 90)
end
function onMouseMoved(mouse,ViewSizeX,X) --- changes direction of aircraft via player moving their cursor.
local BankAngle = GetBankAngle(mouse,ViewSizeX,X)
if fly == true then
turn = true
while turn == true do
if heli ~= nil then
if heli.Parts:FindFirstChild("Engine") ~= nil then
heli.Parts.Engine.BodyGyro.cframe = (mouse * CFrame.Angles(0,0,BankAngle))
--Direction.cframe = (mouse.Hit * CFrame.Angles(0,0,BankAngle))
--heli.Parts.Engine.BodyGyro.cframe = mouse.Hit
end
end
wait(0.1)
end
end
end
local rem=script.Parent.Remotes
rem.onMouseMoved.OnServerEvent:connect(function(player,hit,ViewSizeX,X)
onMouseMoved(hit,ViewSizeX,X)
end)
---the output I am given says that they cannot perform arithmetic on nil values, that being the ViewSizeX and X values.