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 LeftyTwoGuns · Sep 18, 2013 at 08:35 PM · inputwheelcolliderinput.getaxis

Brake on wheel collider "slips"

I have a vehicle using wheel colliders that drives left to right fixed along the X-Axis using Horizontal inputs. I have it scripted to auto-brake when an input key is released, causing it to stop instantly, which is what I want. But sometimes when accelerating with the right key and then switching quickly to accelerate with the left key, the vehicle will continue to slide to the right briefly before accelerating left.

At first I thought it had to do with tire friction/grip but I think it may be an Input problem. Anyone have a suggestion of what I can do to find out what's happening and how to fix it? Here is the script powering the vehicle in case it's useful:

 public class Car : MonoBehaviour {
     
     public Transform centerOfMass;
     
     public WheelCollider FrontLeftWheel;
     public WheelCollider FrontRightWheel;
     public WheelCollider BackLeftWheel;
     public WheelCollider BackRightWheel;
     
     public float[] GearRatio;
     public int CurrentGear = 0;
     
     public float EngineTorque = 230;
     public float MaxEngineRPM = 3000;
     public float MinEngineRPM = 1000;
     private float EngineRPM = 0;
     //public float BrakeTorque = 300;
 
     // Use this for initialization
     void Start () {
         
     if(centerOfMass != null)
         rigidbody.centerOfMass = centerOfMass.localPosition;
          
 
     }
     
      
     
     
     // Update is called once per frame
     void Update () {
         
         EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
         ShiftGears();
         
         audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0f;
         if(audio.pitch > 2.0f) {
             audio.pitch = 2.0f;
         }
         
         FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
         FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
         BackLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
         BackRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis ("Horizontal");
         
         
         
         if(Input.GetAxis ("Horizontal") == 0){
             FrontLeftWheel.brakeTorque = Mathf.Infinity;
             FrontRightWheel.brakeTorque = Mathf.Infinity;
             BackLeftWheel.brakeTorque = Mathf.Infinity;
             BackRightWheel.brakeTorque = Mathf.Infinity;
         }
         
         else {
             FrontLeftWheel.brakeTorque = 0;
             FrontRightWheel.brakeTorque = 0;
             BackLeftWheel.brakeTorque = 0;
             BackRightWheel.brakeTorque = 0;
         }
             
     
     }
     
     void ShiftGears() {
         
         int AppropriateGear;
         
         if(EngineRPM >= MaxEngineRPM){
             AppropriateGear = CurrentGear;
             
             for(int i = 0; i < GearRatio.Length; i ++){
                 if(FrontLeftWheel.rpm * GearRatio[i] < MaxEngineRPM){
                     AppropriateGear = i;
                     break;
                 }
             }
             CurrentGear = AppropriateGear;
         }
         
         if(EngineRPM <= MinEngineRPM){
             AppropriateGear = CurrentGear;
             
             for(int j = GearRatio.Length-1; j >= 0; j --){
                 if(FrontLeftWheel.rpm * GearRatio[j] > MinEngineRPM){
                     AppropriateGear = j;
                     break;
                 }
             }
             CurrentGear = AppropriateGear;
         }
         
             
         
     }    
 }
 
 
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
0

Answer by _MGB_ · Sep 18, 2013 at 08:44 PM

You may be correct with your Input hunch. Try ticking the 'Snap' tickbox, or setting the Gravity value on your Horizontal axis to a very large number - it should return to zero faster.

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 LeftyTwoGuns · Sep 18, 2013 at 09:34 PM 0
Share

I've set Graivty as high a 50,000 but no value seems to make a difference. The braking works about 3/4 of the time, the slip is rare but it's also random and it happening even once is detrimental to gameplay. I can even managed to kind of force it to happen when I get the ti$$anonymous$$g right. Is there some way I can identify when this happens and script it to not happen?

avatar image _MGB_ · Sep 18, 2013 at 09:35 PM 0
Share

Does the 'snap' tickbox make any difference?

avatar image LeftyTwoGuns · Sep 18, 2013 at 09:44 PM 0
Share

Ya, that has always been enabled but when I do disable it the slip happens every time I try to change directions. So I think it's definitely an input problem.

avatar image LeftyTwoGuns · Sep 19, 2013 at 01:21 AM 0
Share

I just set up a public bool to tell me when either left or right input is true and even when the brake "slips" while changing direction, the proper input still lights up when I switch keys, which means that the inputs are triggering and stopping when they should. So I don't really know what's causing the vehicle to slide.

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

16 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

Related Questions

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How to set(raw) 'horizontal' input axis value in script 1 Answer

How to get overall intensity for each Joystick Axis? 4 Answers

input resets after scene change,Held Down Input not responding after scene change. 0 Answers

How to convert Input.GetAxis to Accelerometer control? 2 Answers


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