I mean like it print Left or Right or Forward or Backward without using KeyBegin so it works on mobile too
You could use Humanoid.MoveDirection
.
Yeah but I can’t like make it able to see if its walking forward or backward etc it can be any number not like 1,0,0
Use Humanoid.MoveDirection:Dot(<character root part>.CFrame.LookVector)
.
0 means the player is moving sideways, 1 means the player is moving forward and -1 means the player is moving backwards, all relative to their root part.
If you were to base this on the camera direction, use Humanoid.MoveDirection:Dot(<camera>.CFrame.LookVector)
Again, 0 means the player is moving sideways, 1 means the player is moving forward, -1 means the player is moving backwards. This is relative to the camera as we use the camera instead of the root part of the character.
This works but if you change direction sometimes even walking forward it says sideways
This works but if you change direction sometimes even walking forward it says sideways
That’s because the character IS walking sideways relative to the world axis.
I’m assuming you want to get the character direction based off the direction of the camera, so something like this could help:
local cam = workspace.CurrentCamera -- getting camera
local humanoid = character:WaitForChild("Humanoid") -- getting humanoid
movementDirection = cam.CFrame:vectorToObjectSpace(humanoid.MoveDirection).Unit -- this gets the humanoid movement direction based on the camera rotation
Thanks this works but any idea why when I hold W and D it go to 0 in this script and when I hold W and A it go to -1?
local Humanoid = script.Parent.Parent:WaitForChild('Humanoid')
local Camera = workspace.CurrentCamera
local function MoveDirection()
return math.floor(Humanoid.MoveDirection:Dot(Camera.CFrame.XVector))
end
while wait() do
if tostring(MoveDirection()) == '-1' or tostring(MoveDirection()) == '-2' then
script.Parent:WaitForChild('KeyPressed'):FireServer('A', true)
else
script.Parent:WaitForChild('KeyPressed'):FireServer('A', false)
end
if tostring(MoveDirection()) == '1' or tostring(MoveDirection()) == '2' then
script.Parent:WaitForChild('KeyPressed'):FireServer('D', true)
else
script.Parent:WaitForChild('KeyPressed'):FireServer('D', false)
end
end
You need to do Camera.CFrame.LookVector instead of Camera.CFrame.XVector
function printDirection()
print(movementDirection)
end
–Mouse.KeyDown:Connect(printDirection) – for testing
– Dont add this, just for info
–[[
MoveDirection Unit type vector3
This is a unit vector that describes the movement
direction of the Humanoid.
For example when the Humanoid is moving forward,
MoveDirection will contain the vector (0,1,0).
–]]
(MoveDirection is a property of humanoid)
You could write a function using the previous MoveDirection
value and the new one to find which co-ordinate changed.
Its because Im using it sideways
Yeah, but I need it to work on mobile 2
For detecting sideways movement?
In that case you can just check if the dot product is close to 0
Yeah but I need like Right & Left detection, not just check if walking sideways
Okay, then once we detect sideways movement is close to zero, you could use Humanoid.MoveDirection:Dot(.CFrame.RightVector).
1 means that we are moving right, -1 means we are moving left.
Make sure to add a little margin of error (for example, using < -0.9 instead of == -1)
I know that i am very on time but i think you can just use math.Round(direction)
with humanoid.MoveDirection:Dot(humanoidRootPart.CFrame.LookVector)
Then
1 means forward
-1 means backward
0 and -0 means sideways
or you can use
require(player.PlayerScripts.PlayerModule.ControlModule):GetMoveVector()
it works on all devices
in pc it gives like (1,0,0)
in other devices gives like (0.7, 0, 0.3)
you can check it like
if x>0 then
-- going right
elseif x<0 then
-- going left
else
--not going in this direction
end
(my english is bad sorry)