Detect if the character changed directions suddenly

Hello I’m new to the dev forum so please don’t bully me

I want a way to how to detect if a character made a sudden change in directions,

this will be used in a custom character movement, for example, when the character turns 180 degrees it will slow down their speed and turn them in the other direction

I’ve been trying to use the HumanoidrootPart’s orientation and Humanoid.MoveDirection, comparing the previous orientation to the current one, but I can’t get it right

Any solutions/ tips?

1 Like

if there anything that need clarification pls ask,

So, you are on the right track about using Humanoid.MoveDirection. But, there is a simple way to check if someone turned 180 degrees. You need to know the dot product for this, though, so I suggest you research it if you don’t know about it.

But basically, you can use RunService.RenderStepped to check every frame what the humanoid’s move direction is. Then, you can compare the current move direction to the previous one, by using the dot product, and go on from there:

local counter = 1
local prevMoveDirection = hum.MoveDirection

RunService.RenderStepped()

   if counter % 30 == 0 then

       if humanoid.MoveDirection:Dot(humanoid.MoveDirection) > 0 then --check if the player is not still

            if prevMoveDirection:Dot(humanoid.MoveDirection) < 0.5 then --about 45 degrees

                 --code--

            end

        end

        prevMoveDirection = hum.MoveDirection

    end

    counter = counter + 1

end)

DM me if you need help with it.

3 Likes

this works really well, thanks :grinning:

1 Like