Search Unity

Strange wheel collider behaviour

Discussion in 'Scripting' started by Uberwolfe, Sep 20, 2013.

  1. Uberwolfe

    Uberwolfe

    Joined:
    Jan 25, 2013
    Posts:
    7
    For the last couple of weeks I have been playing around with the wheel colliders with relative success.

    My current project has a tank that drives around with terrain-conforming wheels etc etc.

    Everything works pretty well but one problem I just can't seem to find a solution to.

    When my tank is heading up steep slopes it seems to get bursts of torque - way more than I have set as the limit for each wheel (200rpm). These bursts can go up to 400rpm or greater and send my normally sluggish tank rocketing up steep inclines. While this is amusing for the first hour or so - here I am a few days later scratching my head and become suitably annoyed.

    Does anybody have any ideas?

    Thanks,
    Lukas
     
  2. Akshay_ROG

    Akshay_ROG

    Joined:
    Sep 13, 2013
    Posts:
    48
    I had one problem quite similar with my car where if my car reached a particular speed it's unstoppable, a rocket I might add! but does your tank get normal when it return on the flat ground? is the steep downwards or up? it must be something with calculations. You can provide your piece of code and Community may help...

    Try:
    var throttle : float = Input.GetAxis("Verticle");

    RPM = Mathf.Clamp(Time.deltatime, 0, 200);

    velocity = throttle * RPM; (not tested)
     
  3. Uberwolfe

    Uberwolfe

    Joined:
    Jan 25, 2013
    Posts:
    7
    Hey thanks for the suggestion!

    While it actually works to a degree it doesn't solve the problem entirely.

    While I clamp the RPM and velocity successfully, the problem is RPM reaches that max velocity too fast. So while I don't rocket up slopes super fast anymore there is still some kind of "lurching" effect as the wheel RPMs go from zero to maximum in a split second.

    When I am going downhill or on level slopes everything is fine - it is only really steep inclines. It seems to be especially evident when one or several wheels are suspended in mid-air and then make contact with the ground again....

    Maybe I should just make it a game mechanic? haha jk
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I have the same problem, while I havnt tried to fix it yet, I believe the cause is the torque max's out while the wheel is off the ground offering no resistance.

    I suspect the fix will be to cut all torque if the wheel is off the ground.
     
  5. drag00n

    drag00n

    Joined:
    Apr 20, 2013
    Posts:
    1
    I'm wondering if anyone has found a solution to this problem.
     
  6. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    I am having the same problem. Will try using a raycast to test when wheels are off ground, and if so kill all torque to the wheel.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Wheel colliders have an isGrounded flag... use that instead.
     
  8. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    True.
    I just tried it. Does not work.

    Same effect. Wheels still shoot up slopes....

    Anybody have any ideas??

    So far I have tried adding slip when going up a steep incline - no effect.
    Removing motorTorque when wheels are in the air - no effect.

    RPM still jumps from like 100 to over 500 when just starting to go up an incline...
     
    Last edited: Aug 19, 2014
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    try setting motortorque to zero when it leaves the ground and apply some brake.

    I suspect that once the isgrounded test has failed, its possibly too late and the rpm has already shot up.
     
  10. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    Something like this:
    Code (csharp):
    1. if (!wheelRR.isGrounded){
    2.     wheelRR.motorTorque = 0;
    3.     wheelRR.brakeTorque = 40;
    4.  }
    5. else {
    6.     wheelRR.motorTorque = engineTorque * Input.GetAxis("Vertical");
    7.  
    Doesn't seem to work..

    Have you had any success fixing your code?
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I havnt tried tbh... a different approach might be to manage it based on the rpm as well as isGrounded...

    Code (csharp):
    1.  
    2. float MaxBrakeForce = 75;
    3.  
    4. if(wheelRR.rpm > 100 || !wheelRR.isGrounded)
    5. {
    6.    wheelRR.motortorque = 0;
    7.    wheelRR.braketorque = wheelRR.rpm/100f * MaxBrakeForce;
    8. }
    9.  
     
  12. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    I think the problem with this would be that we are limiting RPM to a set number.

    I can't believe there are so little solutions to this. I guess wheelcolliders just aren't as popular.

    I might have to rework my game because I am making a game with a buggy that is off roading. Lots of slopes, and right now I am just shooting off of slopes. Not fun.
     
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    sure, but its an arbitrary test number for verification purposes.. 100 probably wouldnt be the final number, it would be whatever number feels right to you.

    The wheel collider is behaving exactly as youve told it... if you let it spin up really fast, and its slip isnt set correctly its gonna power the car forward when it makes contact.

    at some point ill experiment with it... have you looked at the Unity car tutorial, see if theres suffers teh same problem?
     
  14. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    I downloaded it this morning but haven't really gotten a chance to play with it. I'll take a look later tonight.

    BTW - do you have a good way of determining the angle of slope a car is currently on? I am using a Unity terrain and would like to know the angle of slope.

    I took a quick video to show exactly what the problem is. Notice how it speeds up going up hills.

     
  15. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  16. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    Thank you I will take a look.

    BTW I am looking at the Unity car tutorial in the asset store and I don't see any wheelcollider objects in that car. So I think they are using their own system and no wheelcolliders.
     
  17. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    What.. really?

    Typical Unity... Here's a car tutorial that doesnt use WheelColliders...

    Think this needs one of those scumbag memes...
     
  18. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    I would just like to update, I looked at Unity's car tutorial. Instead of using wheelcollider.motorTorque, they just add force to the rigidbody. I changed my script as well to work similarly, and it works great! No more zooming up inclines..

    I also found this. Updated 2014 Unity Assets.

    http://blogs.unity3d.com/2014/01/17/new-sample-assets-beta/

    This seems to use motorTorque. I will keep testing.
     
    Last edited: Aug 20, 2014
  19. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    After looking at the Unity 2014 assets... Man they made it really complicated. Sure they are using wheel colliders but making sense of the code is so much more complicated.

    More testing and what I found is: Even in Unity 2014 Beta assets where they are using WheelColliders, the cars zoom up the hills. So it must be some kind of issue with the wheel colliders!

    Looks like I will be using addForce from now on.
     
    Last edited: Aug 20, 2014
  20. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    seems counter intuitive to offer a tutorial with wheel colliders that adds force to the rigidbody... just screams we cant get them to work properly either, so we wont bother.

    see what U5 brings I guess.

    might have to try and figure this problem out myself when I get some time
     
  21. gbabichev1

    gbabichev1

    Joined:
    Aug 9, 2014
    Posts:
    16
    If you do give it a try let me know I am curious to see if you find any solutions.
     
  22. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I had a play around with a physics package I've been building for the asset store, and my vehicles didn't seem to have the problem you mention (losing contact then zooming up the hills). I currently have the suspension as a hinge joint atm, but its still a WIP.

    Couple things to check.
    * Make sure your rigidbody/wheelcollider rigidbody mass is set correctly. My test pickup truck has a mass of 1000, wheel mass of 25.

    * Set the slip settings (note: my car didnt fly even with max grip, so not sure if this a problem anyway)

    However, Ive been looking into what sort of RPM would be expected to reach certain speeds, and mine is probably too low.

    This calculator would probalby be helpful http://www.csgnetwork.com/tirerevforcecalc.html
     
    Last edited: Aug 24, 2014