Applying wheel sideways friction to car

I'm applying sideways friction to my car from each wheel, but for some reason my car starts to accelerate forward instead of having forces acting sideways and stopping it.

This is the script:

    frictionForce = car.transform.TransformDirection(
car.transform.right *
sidewaysFriction *
car.transform.InverseTransformDirection(car.velocity).z
);

car.AddForceAtPosition(springForce - frictionForce * sidewaysFriction, transform.position);


Thanks for any help.

• You should not use car.transform.right (which is in world space) inside TransformDirection (which expects its input in local space). Did you mean to use Vector3.right here instead? Mar 25 '20 at 18:10
• @DMGregory Yes I meant to use Vector3.right. I modified my script but now car started to accelerate forwards, reach some speed and then accelerate backwards, and repeats this forever
– Nick
Mar 26 '20 at 6:07

I've sloved my problem using Debug.DrawRay() to visualize what my vectors looked like.

My error was that in my case, my "side" wasnt Vector3.right, but rather Vector3.forward.

I changed code to:

       frictionForce = car.transform.TransformDirection(
Vector3.forward *
-sidewaysFriction *
car.transform.InverseTransformDirection(car.velocity).z
);
Debug.DrawRay(transform.position, frictionForce, Color.blue);


And it worked.

• Tip: with the scene view translation tool gizmo in Local mode, you can tell which way an object is facing based on the coloured translation arrows when it's selected. The blue arrow is its local forward, and the red arrow is its right. Mar 26 '20 at 10:14
• @DMGregory thanks!
– Nick
Mar 26 '20 at 10:54