Attempting to add wheel friction to a 2d car

I am trying to create wheel friction in a 2d space. I am trying to use the equation f=m^2/r. This equation is used to calculate the forces acting on a vehicle perpendicular to its vector of travel. Currently the code isn’t working correctly. It is hard to explain but basically it adds force bi natter what direction it goes. But I need it to apply the force in the relative x direction. Again it is hard to explain but I would like some advice on what to do. I will post all the code:

``````var power : float = 3;
var maxspeed : float = 5;
var turnpower : float = 2;

var mass : float;

var moving : boolean = false;
var turnright : boolean = false;
var turnleft : boolean = false;

var rightforce : float;
var leftforce : float;

var friction : float = 10;

var curspeed : Vector2;
var curspeedtrue : float;

function Update ()
{
mass = rigidbody2D.mass;

rightforce = -(rigidbody2D.velocity.x);
leftforce = rigidbody2D.velocity.x;

curspeed = Vector2(rigidbody2D.velocity.x, rigidbody2D.velocity.y);
if(curspeed.magnitude > maxspeed)
{
curspeed = curspeed.normalized;
curspeed *= maxspeed;
}

curspeedtrue = rigidbody2D.velocity.x + rigidbody2D.velocity.y;

if(moving == true)
{
rigidbody2D.AddForce(Vector3.right * mass * (curspeed.magnitude * curspeed.magnitude) / turnpower);
}

if(curspeed.magnitude <= 0)
moving = false;
else
moving = true;

if(Input.GetKey(KeyCode.W))
{
}

if(Input.GetKey(KeyCode.S))
{
}

if(moving == true)
{
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * turnpower);
turnleft = true;
}

if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.forward * -turnpower);
turnright = true;
}

}

if(Input.GetKey(KeyCode.A) !=true)
turnleft = false;

if(Input.GetKey(KeyCode.D) !=true)
turnright = false;
}
``````

Ah, okay, well if you are applying the force perpendicular to the wheel then what yo want to do is find the perpendicular vector and then multiply it by the force.

Every transform has three vectors you can get automatically that represent the three axes: right (x), up (y) forward (z). If you modled your tire in the typical fashion ‘facing’ +z then transform.right of the tire object will be the perpendicular vector.

Now, the question is, do you multiply it by your force scalar, or your negative force scalar. This depends on the direction of the force being applied. This depends on your direction of travel. Assuming again the car was molded with its nose facing +z, In a left hand turn, the force is going to be applied in the right (positive) direction. In a right hand turn the force will applied in a left (negative) direction.