How to get the player direction

What if I change camera as HumanRootPart?


Plr = game.Players.LocalPlayer

function prnt(Value)
	Plr.PlayerGui.ScreenGui.T.Text = tostring(Value)
end

game:GetService("RunService").Heartbeat:Connect(function()
	local Char = Plr.Character or Plr.CharacterAdded:Wait()
	
	local rot =
		CFrame.fromMatrix(
			Vector3.new(), 
			Char:WaitForChild("HumanoidRootPart").CFrame.RightVector, 
			Vector3.new(0, 1, 0)
		)

	local relativeDirection = rot:Inverse() *
		Char.Humanoid.MoveDirection
	
	prnt(
		Vector3.new(
			math.floor(relativeDirection.X),
			math.floor(relativeDirection.Y),
			math.floor(relativeDirection.Z)
		)
	)
end)

I tried this but the results are strange, I want to check if the player is moving forward, sideways, and put it all in a vector

Head could move due to animations, it’s better to use the root part in most cases.

I realize this, that is why I said

EDIT: by “looking at” I mean where the head is looking, should’ve worded it better

Relative to what, though?
The camera?
Their front face?
The baseplate?

Relative to the velocity, like something with linear velocity and orientation, when u get the assembly linear velocity, its relative to the world, but I want it relative to the body.

Try HumanoidRootPart.Velocity.Unit

Try replacing math.floor here with math.round.

I should’ve read this first, you could try using an inverse CFrame here on the HRP.

local root = -- humanoid root part

local relativeVelocity = (root.CFrame - root.Position):Inverse() 
    * root.AssemblyLinearVelocity

-- moving forward should result in (0, 0, 16)

Edit: I forgot to take out the HRP’s position, oops.

2 Likes

What do you mean with this? why?

Using math.floor here would mean unit vectors are biased towards the negative side. Something really close to a forward vector like (-0.0001, 0, 0.99998) would be floored to (-1, 0, 0), whereas rounding would result in (0, 0, 1).

If I’m not mistaken, you could use :VectorToObjectSpace to get the velocity relative to the CFrame of whatever part of the character you’d want.

Here’s an example:

local Part = script.Parent

local ObjectSpaceVelocity = Part.CFrame:ToObjectSpace(Part.AssemblyLinearVelocity) -- should return the part's velocity relative to its position
1 Like

it didn’t worked well… amogus

Do you mind telling how it didn’t work? Also how did you use the function?

I think i will use User input service, because it’s to much complex

You’ll have to be more specific, I don’t know what you’re looking for.

You want the move direction… relative to the velocity direction? That doesn’t make sense. Can you be more specific, or maybe give examples?

I want the velocity relarive to the body orientation, if the body are moving toward orientation = (0,0,0) then the value should be (0,0,16) beacause the z is the look vector

Many people have answered how to do this.

For example, @goldenstein64’s solution:

image

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")

while wait() do
	local relativeVelocity = (root.CFrame - root.Position):Inverse() 
		* root.AssemblyLinearVelocity
	print(relativeVelocity)
end

Prints this while walking forward (holding W):

image

i.e. -16 in the Z direction (as expected)

and this while strafing right (holding D in first person):

image

i.e. +16 in the X direction.

@Ferghins’s solution would do the same thing.

Is that not what you’re looking for?

I know you say

But really, “forward” is in the negative Z direction, not positive Z.

1 Like

Yes! that’s exactly what I want, thank you and @goldenstein64 and everyone that helped me.

Feel free to mark @goldenstein64’s reply as the solution, I just copy pasted their code to prove it worked.