Attempting to create a flying script. Problems with mobile controls

Hello! So I have a flying script that moves the player in 1 of 4 directions. I’m having trouble with mobile compatibility because I can’t find a way to reliably determine the player’s movement direction.

I need to find an efficient way to split the player’s thumbstick into 4 direction “zones”. What would be a good way to achieve this?

Thanks in advance. :slight_smile:

1 Like

Humanoids have a property called MoveDirection. If you want the player to only move in axis aligned directions, maybe this will work.

local function getAxisAlignedDirection(moveDir)
    if moveDir == Vector3.new() then
        return moveDir
    end
    local x, z = moveDir.X, moveDir.Z
    if math.abs(x) > math.abs(z) then
        x, z = math.sign(x), 0
    else x, z = 0, math.sign(z)
    end
    return Vector3.new(x, 0, z)
end
1 Like

This has definitely got me closer to the solution I’m looking for, but there is still a problem.

MoveDirection gives different values based on what direction the camera is facing. My fly system moves the player forward relative to their orientation. I need to get the direction of the thumb stick itself. Is there a way to get the thumbsticks X,Y position, and fit it into that system?

Sorry for the bump, I accidentally marked it solved.

I built an Aircraft, compatible with pc and mobile… The mobile touch was very inefficient for me… Cause I wanted the 3 normal effects on aircrafts, Pitch, Roll and Yaw.

So I disabled the default touchpad when the player is on mobile, and created a couple of custom ones, left and right, down corner on the screen. It was easier for me to get proper values to fuel the bodymovers on the plane. Its just a comment about the topic… maybe its totally out of topic… but yeaah! xD That worked for me

1 Like

If the possible movement directions are supposed to be the humanoidRootPart’s look, right, back and left vectors, maybe this will work.

Edit: I realized this wasn’t a good idea.

local function getClosestHrpRelativeDir(hrp, moveDir)
    if moveDir == Vector3.new() then
        return moveDir
    end
    local cf = hrp.CFrame
    local lookVec, rightVec = cf.LookVector, cf.RightVector
    local smallestAngle, closestVec
    for i, v in ipairs({lookVec, rightVec, -lookVec, -rightVec}) do
        local angle = math.acos(v:Dot(moveDir))
        if not smallestAngle or angle < smallestAngle then
            smallestAngle = angle
            closestVec = v
        end
    end
    return closestVec
end
2 Likes

You could try making a X and Z axis - 0 + zone detection by multiplying by the camera cframe lookvector. I have not tried this with X axis but for Z this worked:

forwardmove = character.Humanoid.MoveDirection.Z * currentcam.CFrame.LookVector.Z

since movedirection is world space and could be positive or negative, multiplying it by the camera lookvector Z will make it positive for forward, negative for backward, no matter which way you are facing. 0 for no movement.

I don’t think this will work for X axis now that I think of it, since the camera isn’t facing left or right.

1 Like

I appreciate all of the input, I’m still pretty stumped on the issue though. Anyone have any other ideas?

I found this on the devforum that could help you: How to get the directional vector of a mobile player's thumbstick? - #9 by AronEDEddo

So like this (from the post above):

local GetMoveVector = require(player:WaitForChild("PlayerScripts").PlayerModule:WaitForChild("ControlModule")):GetMoveVector()

So if that works, I can imagine that you can just take the .X and .Y values of the vectors to use as the directional vectors for your movement (by multiplying them by force or something).

Hope this helps!

4 Likes