• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KevDi · Jun 30, 2017 at 08:49 PM · 2dphysics2dsteering

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Confusing Interactions Between 2D Colliders 0 Answers

Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers

keep a 2d sprite in an area via color 0 Answers

Curved wind in 2D game 0 Answers

how to find velocity magnitude relative to transform.right. 2D 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges