I’m trying to change the players walking animations relative to the players’ movement. Here is this drawing for a visual demonstration of the players’ movement"
Now let’s say the player is moving “Forwards”, then it would play the default walking animation, aka animation 1.
In this scenario, all is fine since all characters already do this.
The thing is walking, Backwards, Left, or Right, will all play animation 1, just like walking forwards.
What I’m trying to do is make a script that will change the players’ walking animation based on the direction it is walking relative to what way the HumanoidRootPart is facing. The problem is I don’t know where to start or how I would make this a reality, at my current position in scripting.
:
Do note that I’m using a MeshRig/BoneRig, thus the reason I’m not using any normal or more standard way of doing this, like rotating the Hips Motor6D, or something of the sort.
After hours, of research and scripting, I have come up with this piece of script. And I have come to my senses and decided to share it, I’m now posting this script so no one else has to go through what I did, Enjoy it!
Setup with all commands ready to go, and ready for change if needed.
local Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
-----AnimationHandler
local Animate = script.Parent
local RelativeWalk = script:WaitForChild("RelativeWalk")
---Animations' Names
local WalkF = RelativeWalk.WalkAnimFront
local WalkB = RelativeWalk.WalkAnimBack
local WalkR = RelativeWalk.WalkAnimRight
local WalkL = RelativeWalk.WalkAnimLeft
----MainWalk^^^
local MainWalk = Animate.run.RunAnim
-----Varibles
local RunSer = game:GetService("RunService")
--(*)
local CurrentAnim = nil
-----Functions^^}
function ToWalkRelativeToHRP(Humanoid, Direction, RootCompare)
-----Check if player is moving
if Direction ~= nil then
----X-x-x Continue
local RootFacing = RootCompare.CFrame.LookVector
local RootRight = RootCompare.CFrame.RightVector
local DirectionRelativeToRootFront = RootFacing:Dot(Direction, RootFacing)
local DirectionRelativeToRootSides = RootRight:Dot(Direction, RootRight)
----
local Output = nil
if DirectionRelativeToRootFront <= 1 and DirectionRelativeToRootFront >= 0.75 then
Output = "Forwards"
elseif DirectionRelativeToRootFront >= -1 and DirectionRelativeToRootFront <= -0.75 then
Output = "Backwards"
elseif DirectionRelativeToRootSides <= 1 and DirectionRelativeToRootSides >= 0.75 then
Output = "Right"
elseif DirectionRelativeToRootSides >= -1 and DirectionRelativeToRootSides <= -0.75 then
Output = "Left"
end
return Output
end
end
----------Below heres is not needed but just shows the ways you could use it!
RunSer.RenderStepped:Connect(function()
local ForceVector = ToWalkRelativeToHRP(Humanoid, Humanoid.MoveDirection, HumanoidRoot)
print(ForceVector)
if ForceVector == "Forwards" then
MainWalk.AnimationId = WalkF.AnimationId
elseif ForceVector == "Backwards" then
MainWalk.AnimationId = WalkB.AnimationId
elseif ForceVector == "Right" then
MainWalk.AnimationId = WalkR.AnimationId
elseif ForceVector == "Left" then
MainWalk.AnimationId = WalkL.AnimationId
end
end)
Note… this is just a short version of my script I am still making and will be improved in the future. You can try improving it if you want:
The command Print(Output) connects to the function called “ToWalkRelativeToHPR”
and will give the direction of the players in 4 forms, this can be changed to 8, but I’m still working on that.
Anyways just use Output and it will give you these inputs "Forwards", "Backwards", "Right", "Left".
Anyways that is all, I love helping the community of Roblox Devs, hope this helps
local Direction = nil
local HumanoidRoot = Character:WaitForChild("HumanoidRootPart")
local RootFacing = HumanoidRoot.CFrame.LookVector
local RootRight = HumanoidRoot.CFrame.RightVector
local DirectionRelativeToRootFront = RootFacing:Dot(Humanoid.MoveDirection, RootFacing)
local DirectionRelativeToRootSides = RootRight:Dot(Humanoid.MoveDirection, RootRight)
if DirectionRelativeToRootFront <= 1 and DirectionRelativeToRootFront >= 0.75 then
Direction = "Forwards"
elseif DirectionRelativeToRootFront >= -1 and DirectionRelativeToRootFront <= -0.75 then
Direction = "Backwards"
elseif DirectionRelativeToRootSides <= 1 and DirectionRelativeToRootSides >= 0.75 then
Direction = "Right"
elseif DirectionRelativeToRootSides >= -1 and DirectionRelativeToRootSides <= -0.75 then
Direction = "Left"
elseif (DirectionRelativeToRootFront > 0.5 and DirectionRelativeToRootFront < 0.75)
and (DirectionRelativeToRootSides > 0.5 and DirectionRelativeToRootSides < 0.75) then
Direction = "ForwardRight"
elseif (DirectionRelativeToRootFront > 0.5 and DirectionRelativeToRootFront < 0.75)
and (DirectionRelativeToRootSides > -0.75 and DirectionRelativeToRootSides < -0.5) then
Direction = "ForwardLeft"
elseif (DirectionRelativeToRootFront > -0.75 and DirectionRelativeToRootFront < -0.5)
and (DirectionRelativeToRootSides > -0.75 and DirectionRelativeToRootSides < -0.5) then
Direction = "BackwardLeft"
elseif (DirectionRelativeToRootFront > -0.75 and DirectionRelativeToRootFront < -0.5)
and (DirectionRelativeToRootSides > 0.5 and DirectionRelativeToRootSides < 0.75) then
Direction = "BackwardRight"
end
print(Direction)
slide.Velocity = Humanoid.MoveDirection * Pwr
if Direction == "Forwards" or Direction == "ForwardLeft" or Direction == "ForwardRight" then
slideAnim.AnimationId = "rbxassetid://16850743492"
elseif Direction == "Backwards" or Direction == "BackwardLeft" or Direction == "BackwardRight" then
slideAnim.AnimationId = "rbxassetid://16850816571"
elseif Direction == "Right" then
slideAnim.AnimationId = "rbxassetid://16850895981"
elseif Direction == "Left" then
slideAnim.AnimationId = "rbxassetid://16850899598"
end
```
i also added the other diagonals if anyone finds it useful