Hello!
I got this script from @EgoMoose to save time, where it activates flying for players. It works on PC, however when I try it on mobile, I couldn’t move it with the joystick. How would I go about that?
(Code isnt formatting correctly somehow)
local character = game.Players.LocalPlayer.CharacterAdded:Wait();
local hrp = character:WaitForChild("HumanoidRootPart");
local humanoid = character:WaitForChild("Humanoid");
local animate = character:WaitForChild("Animate");
while (not character.Parent) do character.AncestryChanged:Wait(); end
local idleAnim = humanoid:LoadAnimation(script:WaitForChild("IdleAnim"));
local moveAnim = humanoid:LoadAnimation(script:WaitForChild("MoveAnim"));
local lastAnim = idleAnim;
local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;
local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;
local isFlying = false;
local isJumping = false;
local movement = {forward = 0, backward = 0, right = 0, left = 0};
-- functions
local function setFlying(flying)
isFlying = flying;
bodyGyro.Parent = isFlying and hrp or nil;
bodyVel.Parent = isFlying and hrp or nil;
bodyGyro.CFrame = hrp.CFrame;
bodyVel.Velocity = Vector3.new();
animate.Disabled = isFlying;
if (isFlying) then
lastAnim = idleAnim;
lastAnim:Play();
else
lastAnim:Stop();
end
end
local function onUpdate(dt)
if (isFlying) then
local cf = camera.CFrame;
local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.forward - movement.backward);
if (direction:Dot(direction) > 0) then
direction = direction.unit;
end
bodyGyro.CFrame = cf;
bodyVel.Velocity = direction * humanoid.WalkSpeed * 3;
end
end
local function onJumpRequest()
if (not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
return;
end
if (isFlying) then
setFlying(false);
isJumping = false;
elseif (isJumping) then
setFlying(true);
end
end
local function onStateChange(old, new)
if (new == Enum.HumanoidStateType.Landed) then
isJumping = false;
elseif (new == Enum.HumanoidStateType.Jumping) then
isJumping = true;
end
end
local function movementBind(actionName, inputState, inputObject)
if (inputState == Enum.UserInputState.Begin) then
movement[actionName] = 1;
elseif (inputState == Enum.UserInputState.End) then
movement[actionName] = 0;
end
if (isFlying) then
local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0;
local nextAnim = isMoving and moveAnim or idleAnim;
if (nextAnim ~= lastAnim) then
lastAnim:Stop();
lastAnim = nextAnim;
lastAnim:Play();
end
end
return Enum.ContextActionResult.Pass;
end
-- connections
humanoid.StateChanged:Connect(onStateChange);
game:GetService("UserInputService").JumpRequest:Connect(onJumpRequest);
game:GetService("ContextActionService"):BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward);
game:GetService("ContextActionService"):BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward);
game:GetService("ContextActionService"):BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
game:GetService("ContextActionService"):BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);
game:GetService("RunService").RenderStepped:Connect(onUpdate)```
When I join the game on Mobile, the animations work fine, however when I try to move using the joystick, the player is just frozen in place, with the animation running.
It’s not a bug, you’re doing the formatting wrong. Please refer to roblox’s markdown guide.
It works on desktop correct? Are you using a local script? This likely isn’t something wrong with the flying script but instead the input for mobile if it works for desktop.
The output prints out “forward”, “backwards”, “left”, “right” on desktop, same thing for mobile.
EDIT: Somehow, clicking the Roblox Menu with a Mouse triggers all 4 actionNames, weird.
Where did you put it? for me when i try your script and do what lynnlo said I don’t get a print at all but when i press movements keys on keyboard while testing on mobile then it fires and i could move
Well then, how do I make it so it supports both platforms?? I’m not the person who made the script, @EgoMoose did, so I’m still trying to find out how the script works.
bit late for a reply, but your idea works and I would like to thank you for it. However you were a bit off for constant tracking I did it a little bit different.
local Player = game.Players.LocalPlayer
local MoveVector = require(script.Parent:WaitForChild(‘PlayerModule’):WaitForChild(“ControlModule”))