Best Approach to Implement Direction-Based Walking Animations in an OOP System

Hello! I’m currently working on a combat system, along with the movement system that supports it. Everything is structured using OOP principles. I’m trying to figure out the best way to implement different walking animations depending on the player’s movement direction. My current idea is to detect the change in the humanoid’s movement direction, determine which way the player is walking, and then load the corresponding animation into the animator.

4 Likes

By not using OOP slop for this.

4 Likes

What’s your idea then ? I wanted to keep everything in module script that’s related to movement.

What the heck is OOP? In simple terms and what is the alternative? Give some examples, thank you.

Just create something simple that listens to RunService.PreRender calculates the velocity relative to the RootPart (using VectorToObjectSpace), and adjusts the animation weights directly.

2 Likes

Local scripts?

Using OOP here is completely pointless and shows you don’t actually use your mind when structuring your game. You only use OOP when it makes sense to use it, and even then, you don’t use metatable OOP.

In this case, though, it makes no sense to use OOP.

hey kaelen do you know what a singleton is

A class that can only have one instance.

1 Like

Bloatware in languages outside C++
Well, C++ by itself is bloatware, but at least it compiles, “freeing code from sins”

1 Like

there is probably a faster way to do this that i did not find, but it runs in a parallel connection and takes about .01ms so it’s not a big deal

if you are instead changing animation speed from 1 to -1, do that instead of playing a new directional animation if it is going from forward-backward or left-right

put this in a humanoid movedirection property changed event:

-- create HRP (humanoidrootpart), Humanoid, and Torso variables representing the character objects

local MoveDirection = Humanoid.MoveDirection.Unit 
local RootCF = HRP.CFrame
local RootLookVector = RootCF.LookVector
local TorsoRot = CFrame.fromRotationBetweenVectors(RootLookVector,Torso.CFrame.LookVector)
local WalkType = "Forward"

if MoveDirection.X ~= MoveDirection.X then
	MoveDirection = (RootLookVector * VectorNoY).Unit
end

local ForwardDot = (TorsoRot*RootLookVector):Dot(MoveDirection)
local RightDot = (TorsoRot*RootCF.RightVector):Dot(MoveDirection)

if ForwardDot <= -0.7 then
	WalkType = "Back"
elseif RightDot >= 0.7 then
	WalkType = "Right"
elseif RightDot <= -0.7 then
	WalkType = "Left"
end

???

1 Like

RobloxStudioBeta_5oXpyFzfsG
for easier reference, use the newly made math.isnan that is coming out soon

did bro just say use something that doesn’t exist

mullvadbrowser_UHrVdk0OI4
Newest Patch Notes

“Pending”, meaning it doesn’t yet exist? your point being?

…replace the MoveDirection.X ~= MoveDirection.X with upcoming math.isnan() method whenever it comes out, I guess.

would be really useless to replace with a function call, unless you also want to be safe against infinite number, then you’d replace with math.isfinite(MoveDirection.X)

You are mistaken.
The reason you wanna use metatable OOP is because you will only have one set of functions loaded in the memory, even if you have like 10,000 objects looking them up with __index.

If you use this method on a large scale, then each one of these 10,000 objects will have their own set of functions. Your RAM is gonna be exhausted and your client will crash. It’s just suicide.

Sure, in terms of CPU time, directly accessing the function is slightly faster than checking the table, failing to find it, going to the metatable, checking __index, and finding it there. But you are trading slightly faster CPU execution for much heavier RAM usage and slower instantiation times (let’s not forget that cloning a large table takes longer than creating an empty one).

That’s a little mean.


I do agree that OOP is inappropriate here, though. LocalScripts would do everything just fine. There’s actually a design principle called KISS, which stands for Keep It Simple, Stupid, and it proposes exactly what the name says.
Best of luck to the OP!

No, not really. It stores the references to functions, not the functions themselves, so it is still one function.

Do you want something like this?