How to prevent a player from moving on the Z axis (on mobile)

I want to make a 2d game where the player can only move on the X axis.
I had this working on computer but I have no idea how to do it for Mobile
In a forum post somewhere it said that you are able to change the player MoveVector but I’m only able to get it

2 Likes

this tutorial should work fine on mobile and console, I just tested it out and it works fine

I think I already tried it but I tried it again and I can still move forward and backwards

1 Like
local RunService = game:GetService('RunService')
local ContextActionService = game:GetService('ContextActionService')

local jumping = false
local leftValue, rightValue = 0, 0

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then	
		leftValue = 1
	elseif inputState == Enum.UserInputState.End then
		leftValue = 0
	end
end

local function onRight(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		rightValue = 1
	elseif inputState == Enum.UserInputState.End then
		rightValue = 0
	end
end

local function onJump(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		jumping = true
	elseif inputState == Enum.UserInputState.End then
		jumping = false
	end
end

local function onUpdate()
	if player.Character and player.Character:FindFirstChild('Humanoid') then
		if jumping then
			player.Character.Humanoid.Jump = true
		end
		local moveDirection = rightValue - leftValue
		player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
	end
end

RunService:BindToRenderStep('Control', Enum.RenderPriority.Input.Value, onUpdate)

ContextActionService:BindAction('Left', onLeft, true, 'a', Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
ContextActionService:BindAction('Right', onRight, true, 'd', Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
ContextActionService:BindAction('Jump', onJump, true, 'w', Enum.KeyCode.Space, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)

Try to put this inside a LocalScript inside StarterCharacterScripts like this: fzYGIHX6q4

It works fine for me

2 Likes

oh I did it in StarterPlayerScripts

2 Likes

still not happy with the result tried it on mobile phone emulator and now the circle thing which let’s you move the player doens’t work and now you have random 3 button on the screen for left and right and jump but left and right are place in the wrong order

1 Like

I made it work somehow and this works for all devices.

local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')
local GetMoveVector = require(player:WaitForChild("PlayerScripts"):WaitForChild("ControlScript").MasterControl).GetMoveVector

local function onUpdate()
	local moveVectorDirection = GetMoveVector()
	local moveVector = Vector3.new(moveVectorDirection.X, moveVectorDirection.Y, 0)
	player.Character.Humanoid:Move(moveVector, false)
end

RunService:BindToRenderStep('OnMove', Enum.RenderPriority.Character.Value, onUpdate)
6 Likes