Help with :Dot product

Hello!
I am trying to do this;
If player is moving forward, then value is 1
If backwars, then -1
This is the code;

self.Character.HumanoidRootPart.CFrame.LookVector:Dot(moveDirection)

I guess you know what I mean :smiley:

So, if player moves forward, then it returns 1, if player moves backwards, then returns -1

Yeah, you’ve got it right. What exactly do you need assistance with? Do you just need to define moveDirection? If so, that would just be the normalized velocity of the part (e.g. part.Velocity.Unit).

So you could do the following:

local dot = rootPart.CFrame.LookVector:Dot(rootPart.Velocity.Unit)
if dot > 0 then
	-- Forward
elseif dot == 0 then
	-- Sideways
else
	-- Backward
end

Hiii, thanks for replying, but, when not moving, it returns not a number
-nan(ind)

I think your inputing a nil value if its returning not a number

Ah, fair point. There’s a few ways to check for that. NAN can be checked just by checking if the number is equal to itself. If not, it’s NAN. (e.g. if dot ~= dot then -- it's nan) But you could also just check if the movement is close to 0:

local dot
if rootPart.Velocity.Magnitude > 0.001 then -- Arbitrary small number check
	dot = rootPart.CFrame.LookVector:Dot(rootPart.Velocity.Unit)
end

if not dot then
	-- Not moving
elseif dot > 0 then
	-- Forward
elseif dot == 0 then
	-- Sideways
else
	-- Backward
end
1 Like