How to get the velocity of a part. A positive value begin forward the direction

how to get a positive velocity if the part is moving forward and a negative velocity if the part is moving backward. preferably both of these would on the parts local X axis

It’s as simple as part.Velocity.Magnitude

that works but only for going forward I want it to be negative when going backward however it is still positive

This is a weird issue actually.
A solution may be to detect if the part is moving backwards on the local axis by using CFrames, etc and then flipping the velocity to being negative.

1 Like

The dot product can tell us how similar two vectors are.
By getting the dotproduct between one of the parts basis vectors, we can then tell how the part is moving.

For example if it returns -1 here, then the part is moving backward relative to its lookvector, if 1 then its forward

math.sign(part.CFrame.LookVector(part.Velocity))

The same applies for the x axis

math.sign(part.CFrame.RightVector:Dot(part.Velocity))
2 Likes

This requires vector math with dot product like @Xx1Luffy1xX said.

image

This will get the speed (number) in the direction of where the character is looking.

--put in starter character scripts and walk around
--and look at the print
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")

while true do 
	wait()
	local lookVector = humanoidRootPart.CFrame.LookVector
	local characterVelocity = humanoidRootPart.Velocity
	local resolvedVector = lookVector:Dot(characterVelocity)/lookVector.Magnitude
	print(math.round(resolvedVector))
end

Walking forward will get you 16 which is normal character walkspeed, backwards is -16.

Walking right while in first person will get you 0 because there is no “forward” movement.

Walking diagonally will get you 11 because part of the 16 walkspeed is used to go “right” instead of “forward”.

4 Likes

Thank you so much I still don’t understand the dot product (but I know how to use it thanks to you) since I haven’t taken calculus yet but I definitive will in a few years when I am able to because of you :slight_smile:.

1 Like