How would I go about detecting the direction a player is inputting for their character across platforms?

I’m looking for a way to specifically figure out which direction a player is trying to direct their character, be it on keyboard, gamepad, or mobile. I’m NOT trying to figure out which direction they’re actually moving in.

Any of the solutions I’ve tried (Humanoid.MoveDirection, the lookVector of the HumanoidRootPart, etc.) return the direction the character is actually moving in at that point; this won’t work for my purposes because the way Humanoid rotation works is a smooth transition into the desired movement direction. I’m trying to figure out the raw input direction the player wants to go in.

I’ve done some research on this issue, but I’ve come up dry in my endeavors. Any guidance or pointing in the right direction would be very helpful!

Are you talking about the player’s camera?

Not particularly.

Say if the player is on a keyboard and they’re inputting only the D key. I would want to be able to deduce that they’re inputting a direction that’s exactly 90 degrees to the right of wherever the camera is facing.

That on its own wouldn’t be very hard to figure out, but I’m searching for a solution that can work across keyboard and analog input.

You will have to check their input and compare it to their camera’s angle (only Y).

local _,cangle = camera.CFrame:ToEulerAnglesXYZ()
local idir = Vector2.new(0,1) --your controller’s desired direction

local desiredangle = math.atan2(idir.X,idir.Y)+cangle

1 Like

This should work. Uses the camera cframe as well as the raw movement vector provided by the PlayerModule to get the real movement direction.

local Controller = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
function GetInputDirection()
	local Direction = workspace.CurrentCamera.CFrame:VectorToWorldSpace(Controller:GetMoveVector())
	return if Direction.X > 0 or Direction.Z > 0 then Vector3.new(Direction.X, 0, Direction.Z).Unit else Vector3.zero
end
13 Likes

This is closest to the solution I figured out, thank you! I ended up doing some sneaky things with placeholder CFrames, but I did end up using GetMoveVector in the process.