Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 sattri99 · Dec 15, 2013 at 02:49 PM · rigidbodycollidertorqueunity4.3car physics

How to make 2d Vehicle/car physics in unity 3d 4.3 2d physics?

I want to make a 2d car phyics driving game in the new unity 4.3 engine. I have setup my car sprite and attached rigidbody 2d to it and also setup the wheels using circle collider and spring joints but the only problem i get is how do i move my car according to arrow keys. I have tried Rigidbody2d.AddForce but it only work on specific axis not in the direction where the car is facing. please help me from getting out of this trouble. also thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by LostInCode404 · Dec 17, 2013 at 03:22 PM

I've got the answer. make some variables as shown.

 var force : Vector2;
 //vector for Rigidbody2d.Addforce
 
 var car : Transform;
 //the transform of the car
 
 var power : int;
 //to give magnitude of force
 
 var trigfunction : Vector3;
 //it will give the value of trigonometric functions for the current eulerangles (rotational position) of car
 
 We will use here simple concepts of trigonometry (If you know a little of it, if not just see the way it works)

In function update use this code

 function Update () {
 trigfunction = car.TransformDirection(Vector3.right);
 //now the x component of var trigfunction will give us the value of cos of eulerangle and y will give sin of eulerangle of car
 
 force.Set(trigfunction.x,trigfunction.y);
 //assign the x and y components to the force
 //its a concept of vector physics, x = r cosA , y = r sin A
 
 if (Input.GetKey(KeyCode.W))
 rigidbody2D.AddForce(force*power);
 //assign input key to add forward force
 
 if (Input.GetKey(KeyCode.S))
 rigidbody2D.AddForce(force*power*-1);
 //assign input key to add backward force
 }

if the position is horizontal, sin 0 = 0 and cos 0 = 1 the whole force will be in horizontal.

if the position is vertical. sin 90 = 1 and cos 90 = 0 the whole force will be in vertical.

if the position is anywhere else, the force will be in direction of car in accordance with the values of sin and cos of eulerangles.

It works 101%.

Comment
Add comment · Show 4 · 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 sattri99 · Dec 18, 2013 at 01:23 PM 0
Share

Thanks,3d Scientist it works 101% as you said. It is the best solution for this problem as it seems to me. I just attached this script to car and give the car transform and the car runs on my keys.keep it up.again thanks for the script

avatar image sattri99 · Jan 01, 2014 at 02:58 AM 0
Share

I've made a good 2D Car physics game from your idea and uploade it on my new blog (with source code link). You can download it and check it out www.physicist3d.blogspot.com

avatar image falconer · Dec 01, 2014 at 05:27 AM 0
Share

Nice work... But I still feel there is a lot to go before you can say its complete. I have found another article by The Game Contriver which is quite realistic than the ones I have come across so far. If anyone needs it, check the link below

Unity 2D Car Physics

avatar image gulfam131 · Jan 17, 2015 at 08:14 PM 0
Share

Thanks, Its working. :)

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Detect collider when same object contains more than on collider 1 Answer

Is Triggered Collider Issue 1 Answer

OnCollisionEnter isn't working between two RigidBodies 1 Answer

How does object shape influence its collision? 3 Answers

Rigidbody not sleeping 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