# Velocity.Magnitude that's relative to a part's local axis

You can write your topic however you want, but you need to answer these questions:

1. What do you want to achieve? Keep it simple and clear!
I want to find a way to calculate the speed of an object’s movement, relative to their front side / local axis. Like in the example from below.

2. What is the issue? Include screenshots / videos if possible!
Velocity.Magnitude gives an absolute number (not positive or negative) so there is no way to differentiate between directions.
And also, as this would be part of a vehicle that is not limited to just one axis of movement, I cannot just read the position property, as that is based on the World Axis.
Also also, I am aware of this solution, but my current script doesn’t really fit another loop in it. I made this post to look for alternative solutions to this, too.

1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
No, but I’m thinking that some constraints might help.

My use case is in a rail vehicle that, in order to imitate a faster braking speed, spins the wheels counter-clockwise to slow down faster. Problem is, this inevitably leads to it going backwards, which is really against what I already defined with my chassis. I wanted to run a check whenever it is applying this unconventional brake that would stop the process if the main part of the model starts going backwards.

Speed = Distance/Time

You can use this to calculate your speed. Just to give an idea

``````while task.wait(1) do
speed = (newPosition - oldPosition) / 1 -- divided by one because 1 second has passed, we are using task.wait(1)
oldPosition  = newPosition
end
``````
1 Like

Thank you for the suggestion. I forgot to mention that, due to the way my script works, this algorithm wouldn’t fit in too well. If nothing else pops up then I’m going to have to make it work, though…

If you want to find the front of the part, you can take a look at `LookVector`. `CFrame.LookVector` returns a unit length (1 stud long) vector protruding from where the `CFrame` is looking. This can be useful because if you divide the direction the part is moving in (assuming that it’s either forward or backwards), it will return the direction it’s travelling.

``````local movement = (Part.Velocity / Part.CFrame.LookVector).Z
--[[
Lets say LookVector is [1, 0, 0] and the Velocity is [-20, 0, 0].
You would get a result of [-20] from running this code
--]]
``````
1 Like