Im trying to make a vehicle system, but I dont want to use roblox vehicle seats cause they confuse me a lot.
Anyhow beside the point I need to make mobile controls and I think using a joystick would help a lot for mobile, so how can I tell which way its pointed to pinpoint which way the vehicle will move,
how can I do this?
I’ve tried looking for posts on this topic but couldnt find any
1 Like
For mobile players, Humanoid.MoveDirection will always be a unit vector corresponding to the direction of the thumbstick.
Even if they are in a seat? That was my main concern
Yes, regardless of circumstance, MoveDirection will always correspond to the thumbstick direction - whether they are in a seat, anchored, have 0 WalkSpeed, etc.
1 Like
Ok so the numbers for move direction are wild, and change based on the direction you are facing, how can I tell if its just straight up moving forward, or left, or right, etc?
Like is there some form of formula I could use to convert it to a readable sorta thing ig.
Idk Im just throwing around ideas
The MoveDirection is just an ordered pair (direction.X, direction.Z) that can be plotted on a graph where (0, 0) represents the position of your character and the vertices on each end of the axes represent a relative direction.
If you want to limit to just one of four directions (forward, backward, left, right), you can do distance checks to determine which is closest to the current MoveDirection.
I can do distance checks, but it wont give me the exact direction they are going exactly how I want.
For example if I walk forwards, the number could either end up being a negative or positive number based on the direction you are facing, so it would be hard to tell if they are exactly moving forward.
MoveDirection is a vector represented in world space. You can convert it to object space like this:
character.HumanoidRootPart.CFrame:VectorToObjectSpace(humanoid.MoveDirection)
Here's my attempt at coding this for you to reference
local Players = game:GetService("Players")
local directions = {
Forward = Vector3.new(0, 0, -1);
Backward = Vector3.new(0, 0, 1);
Right = Vector3.new(1, 0, 0);
Left = Vector3.new(-1, 0, 0)
}
function GetDirectionFromVector(vector)
local closestName, closestDistance
for name, reference in directions do
local distance = (vector - reference).Magnitude
if closestDistance and closestDistance < distance then
continue
end
closestDistance = distance
closestName = name
end
return closestName
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
local direction = humanoid.MoveDirection
if direction == Vector3.zero then
return
end
print(GetDirectionFromVector(humanoidRootPart.CFrame:VectorToObjectSpace(direction)))
end)
end)
end)
(side note: My initial graph was wrong, (0, 1) should be Backward and (0, -1) should be Forward)
2 Likes
This should work.
local roundhumanoidmd = Vector3.new(math.round(humanoid.MoveDirection.X), math.round(humanoid.MoveDirection.Y), math.round(humanoid.MoveDirection.Z))
local roundlookvector = Vector3.new(math.round(char.HumanoidRootPart.CFrame.LookVector.X), math.round(char.HumanoidRootPart.CFrame.LookVector.Y), math.round(char.HumanoidRootPart.CFrame.LookVector.Z))
if roundhumanoidmd == roundlookvector then
--code
end
1 Like
if you want the raw input data, you can get that through the player control module
local plr = game.Players.LocalPlayer
local plrControls = require(plr.PlayerScripts.PlayerModule.ControlModule)
local moveDir = Vector2.new(plrControls:GetMoveVector().X, -plrControls:GetMoveVector().Z)
this works not only for mobile, but for xbox too.
1 Like
Ok after testing it, it seems based off of the direction the camera is facing for some reason, so If I point the camera in one direction its different if it was facing another, know anyway to go about fixing that?