Walking direction relative to player?

I feel like this is super simple, but I can’t figure it out :crazy_face:

How do I get the walking direction of the player in local coordinates, so for example if the player is in shift lock mode and he moves left, no matter his orientation in the world I need it to print that he’s moving left. I’ve tried Part.Velocity, Humanoid.MoveDirection etc. but all of those are in world coordinates :confused:

9 Likes

http://wiki.roblox.com/index.php?title=CFrame#vectorToObjectSpace

Call on CFrame of part (HumanoidRootPart) with vector in worldspace

12 Likes

You can also get the control vector by calling MasterControl:GetMoveVector(). MasterControl is a module located in PlayerScripts > ControlScript > MasterControl during run-time, unless you’ve overridden ControlScript.

EDIT: don’t do this, the PlayerScripts are changing structure so this isn’t best practice anymore.

For example, if you move forward on keyboard, the move vector will be (0,0,-1), and if you move to the side on keyboard it’d be (1,0,0) or (-1,0,0), or i.e. (1,0,1) if they are moving both sideways and backward. For mobile/controller, the vector would have variable length, as you have analog input there. The X/Z components show the movement direction. It’s also cross-platform.

7 Likes

Did not know this.

I really wish they rewrote all the character control, camera, and chat scripts to be more user friendly.

6 Likes

The standard Roblox control script, unmodified, passes “true” as the second argument to Humanoid:Move(), so the moveVector is always interpreted in the camera’s coordinate space. In shift lock, this is also your character’s orientation, as they are forcibly aligned.

But when you’re not in shift lock, you need to pick what you mean by “local coordinates”. For example, if your character is running towards the camera, and you push the D key, they are going to move right in camera space, but from the character’s point of view it’s a left turn. So if you compare the character’s velocity against their humanoid root part orientation, it might not be what you want (you may want camera-relative “right”). Just something to keep in mind.

The second gotcha with using the moveVector directly is that it is only the desired movement direction; if your character is up against an obstacle, they can be stationary yet still have a non-zero moveVector. So, if what you really need to know is literally what direction they are moving, this is not enough information. In fact, you character can be moving in the opposite direction as the moveVector, since changes to velocity are not instantaneous in response to user input, it can take a few frames to reverse direction, for example. My advice would be to use the moveVector if what you care about is the user control direction, and use the character’s root part velocity if what you care about is actual movement direction and speed.

I forgot to put a solution, but @TaaRt’s answer was what I was looking for :smile:

1 Like