I’m trying to make it that when the player moves side to side, the legs rotate to the direction they’re going to. The only problem is that MoveDirection is world space, not object space. I’ve tried using velocity, it doesn’t output any velocity though, I tried checking the devforums, but I couldn’t find anything.
Here’s an example of what I want: (Recorded on the game town, the link to the game is here.)
The output of my code: (using MoveDirection)
Code:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
if humanoid.RigType ~= Enum.RigType.R15 then
local torso = character:WaitForChild("Torso")
local leftHip = torso:WaitForChild("Left Hip")
local rightHip = torso:WaitForChild("Right Hip")
local originalLeftCFrame, originalRightCFrame
originalLeftCFrame = leftHip.C0
originalRightCFrame = rightHip.C0
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
local moveDirection = humanoid.MoveDirection
local xMove = humanoid.MoveDirection.X
leftHip.C0 = originalLeftCFrame * CFrame.Angles(0, xMove / 4, 0)
rightHip.C0 = originalRightCFrame * CFrame.Angles(0, xMove / 4, 0)
end)
end
end)
end)
local run = game:GetService('RunService')
local HumanoidRootPart = script.Parent:WaitForChild('HumanoidRootPart')
run.RenderStepped:Connect(function(dt)
local velocity = HumanoidRootPart.CFrame:Inverse() * (HumanoidRootPart.Position + HumanoidRootPart.Velocity)
local yDirection = math.atan2(velocity.X, -velocity.Z)
local roundedDirection = math.ceil(math.deg(yDirection) - 0.5)
if roundedDirection > 0 and roundedDirection < 180 then --strafing right
--AnimationHandler.Strafing = true
if script.Parent.Humanoid.MoveDirection.Magnitude > 0 then
-- do something
else
end
elseif roundedDirection < 0 and roundedDirection > -180 then --strafing left
--AnimationHandler.Strafing = true
if script.Parent.Humanoid.MoveDirection.Magnitude > 0 then
else
--do something
end
elseif roundedDirection <= -135 or roundedDirection >= 135 then
--AnimationHandler.WalkingBackwards = true
if script.Parent.Humanoid.MoveDirection.Magnitude > 0 then
-- do something
yDirection += math.rad(180)
end
else
end
end)
basically it check in what direction a player is moving if this can help
I believe there is a function in the player module that actually returns the moveVector according to the player;
local modulescript = require(game:GetService("Players"):WaitForChild("PlayerScripts"):WaitForChild("PlayerMovement")) --this isn't the actual name of the movement script but if you dig around you can find it
local moveVector = modulescript:GetVector()
print(moveVector.Z) --> -1 for walking backwards; 0 for walking sideways; 1 for walking forwards