• 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

2D Animation does not start 1 Answer

Please help. I can't stop this light from moving!!! 1 Answer

2D Shooting right only. Doesn't flip direction with character 1 Answer

Instantiate object that points to the direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges