# How can i check if a body is moving forward or backward from its velocity

I’m currently trying to re-make my car script and i’m having issues with the velocity of the car,
I’m trying to make it be able to move backward and forward and also move in the front / backward direction of the car.

This is what i have so far:

``````        local LookVector = -Chassis.CFrame.LookVector
local Throttle = DriveSeat.Throttle
local Steer = DriveSeat.Steer
local Acceleration = LookVector * (CarConfig.Acceleration * step) * Throttle

if Chassis.Velocity.Magnitude < CarConfig.MaxSpeed then
Chassis.Velocity += Acceleration
end

local x = math.clamp((Chassis.Velocity.Magnitude / ((CarConfig.MaxSpeed * 1.2) / 2)), 0, 1)
local TurnFactor = math.sin(x * math.pi)

if (Chassis.RotVelocity.Magnitude / CarConfig.TurnSpeed) < TurnFactor then
local y = -Steer * TurnFactor * CarConfig.TurnAcceleration * step
Chassis.RotVelocity += Vector3.new(0,y,0)
end
``````

The problem with this is, it makes the car almost slide on the surface it’s on, the car doesn’t move forward relative to how it’s faced and it makes it look like the car has no grip, any help / articles would be appreciated

2 Likes

To be able to know the direction something is moving based on where its facing, you need to get it’s relative velocity. You can do this with the CFrame function CFrame:VectorToObjectSpace(). So you’d do it like this.

``````velocity = Chassis.CFrame:VectorToObjectSpace(Chassis.Velocity)
``````

I believe the Z component is forward and backward, but it could be X.

4 Likes

I’ll give this a shot right now, give me one second

EDIT: That worked! Can’t believe that was it, thanks so much