Top Down Car 2d physic with steeringwheel controller
I have the following script for the controlling of my top down 2d car.
float h = -Input.GetAxis("Horizontal");
Vector2 speed = transform.up * app.model.Speed;
Log("App Speed: " + app.model.Speed);
Debug.Log("Magni: " + app.model.car.rb.velocity.magnitude);
Debug.Log("Speed: " + speed);
if (!(app.model.car.rb.velocity.y >= app.model.Speed))
{
app.model.car.rb.AddForce(speed);
}
float direction = Vector2.Dot(app.model.car.rb.velocity, app.model.car.rb.GetRelativeVector(Vector2.up));
if (direction >= 0.0f)
{
app.model.car.rb.rotation += h * 3 * (app.model.car.rb.velocity.magnitude / 5.0f);
//rb.AddTorque((h * steering) * (rb.velocity.magnitude / 10.0f));
}
else
{
app.model.car.rb.rotation -= h * 3 * (app.model.car.rb.velocity.magnitude / 5.0f);
//rb.AddTorque((-h * steering) * (rb.velocity.magnitude / 10.0f));
}
Vector2 forward = new Vector2(0.0f, 0.5f);
float steeringRightAngle;
if (app.model.car.rb.angularVelocity > 0)
{
steeringRightAngle = -90;
}
else
{
steeringRightAngle = 90;
}
Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;
Debug.DrawLine((Vector3)app.model.car.rb.position, (Vector3)app.model.car.rb.GetRelativePoint(rightAngleFromForward), Color.green);
float driftForce = Vector2.Dot(app.model.car.rb.velocity, app.model.car.rb.GetRelativeVector(rightAngleFromForward.normalized));
Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);
Debug.DrawLine((Vector3)app.model.car.rb.position, (Vector3)app.model.car.rb.GetRelativePoint(relativeForce), Color.red);
app.model.car.rb.AddForce(app.model.car.rb.GetRelativeVector(relativeForce));
It's the one from this question: http://answers.unity3d.com/answers/1093132/view.html If i try it with the keyboard it works very well. If i now connect the steering wheel (Logitech G29) it's hard to controll the car because if the steering wheel automatically rotates to the starting position the car still points in the direction where it was when i let go the steering wheel and let it rotate back to its starting position. How can i accomplish that the car also rotates back to the start position (pointing upwards?)
Your answer
Follow this Question
Related Questions
I don't know how to add acceleration to my 2D car script..?? 0 Answers
How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers
2d gameobjects stuttering 0 Answers
how to make this top down 2d car drift right. 0 Answers
How would I go around making a gravity gun type system in 2D? 0 Answers