0
\$\begingroup\$

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);

enter image description here

Thanks for any help.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Mar 25 '20 at 18:10
  • \$\begingroup\$ @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 \$\endgroup\$
    – Nick
    Mar 26 '20 at 6:07
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Mar 26 '20 at 10:14
  • \$\begingroup\$ @DMGregory thanks! \$\endgroup\$
    – Nick
    Mar 26 '20 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.