Hi,
I’m trying to create a flight script that works on both PC and mobile.
The problem is that I don’t understand how to handle movements based on the thumbstick.
Here is the already working code for the PC version.
local function getMovementDirection()
local moveDirection = Vector3.new()
local cameraCFrame = Camera.CFrame
-- Get movement direction from Humanoid
local moveDir = Humanoid.MoveDirection
-- Calculate starting vectors from Camera
local forwardVector = cameraCFrame.LookVector
local rightVector = cameraCFrame.RightVector
local upVector = cameraCFrame.UpVector
if mobile then
--HANDLE MOVEMENT WITH ROBLOX DEFAULT THUMBSTICK
else
--handle movements with keyboard
local moveForward = (UserInputService:IsKeyDown(Enum.KeyCode.W) and 1 or 0) +
(UserInputService:IsKeyDown(Enum.KeyCode.S) and -1 or 0)
local moveRight = (UserInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0) +
(UserInputService:IsKeyDown(Enum.KeyCode.D) and 1 or 0)
local moveUp = (UserInputService:IsKeyDown(Enum.KeyCode.Space) and 1 or 0) +
(UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and -1 or 0)
-- Horizontal direction based on camera
local horizontalDirection = (forwardVector * moveForward) + (rightVector * moveRight)
-- Vertical direction based on camera
local verticalDirection = upVector * moveUp
moveDirection = horizontalDirection + verticalDirection
end
return moveDirection
end
-- Movement and updating function
game:GetService("RunService").RenderStepped:Connect(function()
if isFlying then
Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
BodyGyro.CFrame = Camera.CFrame
local moveDirection = getMovementDirection()
BodyVelocity.Velocity = moveDirection * 120
end
end)