• 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 bhavesh123 · Apr 18, 2014 at 07:00 PM · speedcarlimitlimitationsexceed

How do I limit the speed of my car?

I am making a car game and i cannot limit the speed, but it the speed just goes unlimited. Below is the code which is not limiting my speed and converts the speed to kmph.

  function Control (){
     currentSpeed = 2*22/7*WheelRL.radius*WheelRL.rpm*60/1000; //this converts the speed to kmph
     currentSpeed = Mathf.Round(currentSpeed); //this converts the speed to kmph
     if (currentSpeed < topSpeed && currentSpeed > -maxReverseSpeed){ //these lines limit my speed
     WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
     WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
     }
     else {
     WheelRR.motorTorque =0;
     WheelRL.motorTorque =0;
     }

Below is my whole car control code:-

 #pragma strict
 
 var WheelFL : WheelCollider;
 var WheelFR : WheelCollider;
 var WheelRL : WheelCollider;
 var WheelRR : WheelCollider;
 var WheelFLTrans : Transform;
 var WheelFRTrans : Transform;
 var WheelRLTrans : Transform;
 var WheelRRTrans : Transform;
 var lowestSteerAtSpeed : float = 50;
 var lowSpeedSteerAngel : float = 20;
 var highSpeedSteerAngel : float = 5;
 var decellarationSpeed : float = 30; 
 var maxTorque : float=50;
 var currentSpeed : float;
 var topSpeed : float = 150;
 var maxReverseSpeed : float = 50;
 var backLightObject : GameObject;
 var idleLightMaterial : Material;
 var brakeLightMaterial : Material;
 var reverseLightMaterial : Material;
 private var mySidewayFriction : float;
 private var myForwardFriction : float;
 private var slipSidewayFriction : float;
 private var slipForwardFriction : float;
 
 function Start () {
 rigidbody.centerOfMass.y = -0.9;
 }
 
 function SetValues (){
 myForwardFriction  = WheelRR.forwardFriction.stiffness;
 mySidewayFriction  = WheelRR.sidewaysFriction.stiffness;
 slipForwardFriction = 0.05;
 slipSidewayFriction = 0.085;
 }
 
 function FixedUpdate () {
 Control();
 }
 
 function Control (){
 currentSpeed = 2*22/7*WheelRL.radius*WheelRL.rpm*60/1000; //this converts the speed to kmph
 currentSpeed = Mathf.Round(currentSpeed); //this converts the speed to kmph
 if (currentSpeed < topSpeed && currentSpeed > -maxReverseSpeed){ // this line and the below lines is the code which limits my speed
 WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
 WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
 }
 else {
 WheelRR.motorTorque =0;
 WheelRL.motorTorque =0;
 }
 
 if (Input.GetButton("Vertical")==false){
 WheelRR.brakeTorque = decellarationSpeed;
 WheelRL.brakeTorque = decellarationSpeed;
 }
 else{
 WheelRR.brakeTorque = 0;
 WheelRL.brakeTorque = 0;
 }
 
 WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
 WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
 WheelFR.steerAngle = 20 * Input.GetAxis("Horizontal");
 WheelFL.steerAngle = 20 * Input.GetAxis("Horizontal");
 
 var speedFactor = rigidbody.velocity.magnitude/lowestSteerAtSpeed;
 var currentSteerAngel = Mathf.Lerp(lowSpeedSteerAngel,highSpeedSteerAngel,speedFactor);
 currentSteerAngel *= Input.GetAxis("Horizontal");
 WheelFL.steerAngle = currentSteerAngel;
 WheelFR.steerAngle = currentSteerAngel;
 }
 
 function Update(){
 WheelFLTrans.Rotate(WheelFL.rpm/60*360*Time.deltaTime,0,0);
 WheelFRTrans.Rotate(WheelFR.rpm/60*360*Time.deltaTime,0,0);
 WheelRLTrans.Rotate(WheelRL.rpm/60*360*Time.deltaTime,0,0);
 WheelRRTrans.Rotate(WheelRR.rpm/60*360*Time.deltaTime,0,0);
 WheelFLTrans.localEulerAngles.y = WheelFL.steerAngle - WheelFLTrans.localEulerAngles.z;
 WheelFRTrans.localEulerAngles.y = WheelFR.steerAngle - WheelFRTrans.localEulerAngles.z;
 BackLight();
 }
 
 function BackLight (){
 if (currentSpeed > 0 && Input.GetAxis("Vertical")<0){
 backLightObject.renderer.material = brakeLightMaterial;
 }
 else if (currentSpeed < 0 && Input.GetAxis("Vertical")>0){
 backLightObject.renderer.material = brakeLightMaterial;
 }
 else if (currentSpeed < 0 && Input.GetAxis("Vertical")<0){
 backLightObject.renderer.material = reverseLightMaterial;
 }
 else{
 backLightObject.renderer.material = idleLightMaterial;
 }
 }
Comment
Add comment · Show 5
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 EvilTak · Apr 19, 2014 at 07:55 AM 0
Share

You can edit the rigidbody.drag values to limit your speed.

avatar image bhavesh123 · Apr 19, 2014 at 03:09 PM 0
Share

Thanks Evil Tak it worked and i wanted to ask one more thing that my car goes to a little left automatically.

avatar image EvilTak · Apr 19, 2014 at 03:49 PM 0
Share

This is a known bug of the sideways slip of wheelcolliders. You can set the sideways slip higher, but that results in a sticky or too grippy wheel. This usually occurs while driving on a flat plane, but on rough terrain you can't notice it much. Or you may use another wheel alternative, like UnityCar's wheel, by using physics raycasts. Personally, I feel that the built in wheel collider's are too unrealistic. $$anonymous$$aybe the unity 5 update with PhysX 3.3 will be different.

avatar image tallieke · Jul 02, 2015 at 09:07 AM 0
Share

I like flat tutorial too!

avatar image Harraz · Dec 10, 2015 at 08:18 PM 0
Share

rigidbody.drag values.... thanks...

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

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

23 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

Related Questions

Pathfinding with specific end-direction and turning speed limit 0 Answers

How make breaks and maximum speed 0 Answers

Question about the speed in the cartutorial 0 Answers

How to limit the force from player input without limiting speed 2 Answers

Reduce steerangle at high speeds 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