• 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 /
avatar image
0
Question by Major · May 06, 2014 at 12:12 AM · 2ddirectioncaryx

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);
         //rigidbody2D.AddForce(-(Vector2.right) * mass * (curspeed.magnitude*curspeed.magnitude));
     }
     
     if(curspeed.magnitude <= 0)
         moving = false;
         else
         moving = true;
         
     if(Input.GetKey(KeyCode.W))
     {
         rigidbody2D.AddForce(transform.up * power);
     }
     
     
     if(Input.GetKey(KeyCode.S))
     {
         rigidbody2D.AddForce(-(transform.up) * power);
     }
     
     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;
 }

  
Comment
Add comment · Show 2
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
avatar image Jeff-Kesselman · May 06, 2014 at 12:40 AM 0
Share

Im confused, shouldn't wheel friction be applied perpendicular to the direction the wheel is traveling in?

avatar image Major · May 06, 2014 at 01:05 AM 0
Share

yes, I miss typed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 06, 2014 at 01:48 AM

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.

Comment
Add comment · Show 3 · Share
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
avatar image Major · May 06, 2014 at 02:26 AM 0
Share

I understand everything up to finding the correct force to apply in the perpendicular direction. This was the theory I have been working with.

avatar image Jeff-Kesselman · May 06, 2014 at 02:29 AM 0
Share

Ah, well its your formula. If its not right or if you don't know what it means, then Id have to do research to see what I came up with in terms of a formula. As you presented it above Im not even sure what all the terms mean.

avatar image Major · May 06, 2014 at 03:12 AM 0
Share

well I can tell you now that m=mass, v=velocity and r=radius of the turn. so at the moment, this is just a big circle.

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

21 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

Related Questions

2d rigidbody drag on x, y axis 0 Answers

Instantiate after setting values? 0 Answers

Rotate an arrow object based on addedForce 1 Answer

player shoot projectile in one direction (x) -1 Answers

Relation between two 3D vectors that are in the same plane, to translate to 2D 4 Answers

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