-
What do you want to achieve? Keep it simple and clear!
I want to get player direction vector3, like if he/she is going forward, the vector would be 0,0,1
and if he/she was going to left and front, it would be 1,0,1 -
What is the issue? Include screenshots / videos if possible!
I can’t get this vector. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to use CFrame:ToObjectSpace() with HumanRotPart linear velocity and HRP orientation
but with no succed
Humanoids have a property called ‘MoveDirection’ which is not in relation to the root part’s orientation.
You could also check the RootPart’s CFrame.LookVector to find what direction the character is pointing in.
also if you wanted to find the direction the character is looking at, you can get the LookVector of the character’s head
I already tried MoveDirection but its like HPR.Orientation.Unit but I think look vector is useful
Yes but how can I connect look vector with the linear velocity?
Its possible to get the user’s rotation matrix interpolated to the front, right or top of the character. The rotation matricie should be used as a unit in another vector/cframe
CFrame.LookVector
CFrame.RightVector
CFrame.UpVector
You could also use Humanoid.MoveDirection.
A really common thing done with vehicles is to multiply the LookVector of an BasePart and feed it into a BodyVelocity as such:
Mover.Velocity = Mover.Parent.CFrame.LookVector * 80
This is updated every frame so that it respects steering.
How about something like this (assuming your camera doesn’t have roll to it):
local rot = CFrame.fromMatrix(Vector3.new(), workspace.CurrentCamera.RightVector, Vector3.new(0, 1, 0))
local relativeDirection = rot:Inverse() * humanoid.MoveDirection
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.
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
it didn’t worked well… amogus