Search Unity

How to enable physic material on Wheel colliders !

Discussion in 'Scripting' started by marclar83, Oct 2, 2016.

  1. marclar83

    marclar83

    Joined:
    Jun 7, 2012
    Posts:
    16
    If you're using the wheel collider and want to use the physic material to create different surfaces, well you'll found out it doesn't work ! Wheel colliders have special parameters to handle the wheel stiffness that ignore Physic Materials !

    So I basically made a little script a year ago to solve my problem and then I lost it... I finally found it today while looking through a old external hard drive I was about to format, so I decided it would be better to share it before it got lost forever !

    You have to apply this script to all your wheels !
    Name this script: enableWheelPhysicMaterial.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enableWheelPhysicMaterial : MonoBehaviour
    5. {
    6.     private WheelCollider wheel;
    7.     void Start()
    8.     {
    9.         wheel = GetComponent< WheelCollider >();
    10.     }
    11.     // static friction of the ground material.
    12.     void FixedUpdate()
    13.     {
    14.         WheelHit hit;
    15.         if (wheel.GetGroundHit(out hit))
    16.         {
    17.             WheelFrictionCurve fFriction = wheel.forwardFriction;
    18.             fFriction.stiffness = hit.collider.material.staticFriction;
    19.             wheel.forwardFriction = fFriction;
    20.             WheelFrictionCurve sFriction = wheel.sidewaysFriction;
    21.             sFriction.stiffness = hit.collider.material.staticFriction;
    22.             wheel.sidewaysFriction = sFriction;
    23.         }
    24.     }
    25. }
    The original script was written in JavaScript so...

    Code (JavaScript):
    1. #pragma strict
    2. public var wheel: WheelCollider;
    3. function Start() {
    4.     wheel = GetComponent.<WheelCollider>();
    5. }
    6. // static friction of the ground material.
    7. function FixedUpdate() {
    8.     var hit: WheelHit;
    9.     if (wheel.GetGroundHit(hit)) {
    10.         var fFriction: WheelFrictionCurve = wheel.forwardFriction;
    11.         fFriction.stiffness = hit.collider.material.staticFriction;
    12.         wheel.forwardFriction = fFriction;
    13.         var sFriction: WheelFrictionCurve = wheel.sidewaysFriction;
    14.         sFriction.stiffness = hit.collider.material.staticFriction;
    15.         wheel.sidewaysFriction = sFriction;
    16.     }
    17. }
    Enjoy !!
     
    CandraWi, matharoo, EZaca and 8 others like this.
  2. JamesBateman

    JamesBateman

    Joined:
    Jul 4, 2018
    Posts:
    1
    I know this post is like 5 years old, but it's MASSIVELY helpful, thanks!!!!
     
  3. ahsen35813

    ahsen35813

    Joined:
    Sep 15, 2019
    Posts:
    9
    Slight modification so as not to override all your wheel's settings

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enableWheelPhysicMaterial : MonoBehaviour
    5. {
    6.     private WheelCollider wheel;
    7.  
    8.  
    9.     private float originalSidewaysStiffness;
    10.     private float originalForwardStiffness;
    11.  
    12.    
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         wheel = GetComponent<WheelCollider>();
    18.  
    19.         originalSidewaysStiffness = wheel.sidewaysFriction.stiffness;
    20.         originalForwardStiffness = wheel.forwardFriction.stiffness;
    21.     }
    22.     // static friction of the ground material.
    23.     void FixedUpdate()
    24.     {
    25.         WheelHit hit;
    26.         if (wheel.GetGroundHit(out hit))
    27.         {
    28.             WheelFrictionCurve fFriction = wheel.forwardFriction;
    29.             fFriction.stiffness = hit.collider.material.staticFriction * originalForwardStiffness;
    30.             wheel.forwardFriction = fFriction;
    31.  
    32.  
    33.             WheelFrictionCurve sFriction = wheel.sidewaysFriction;
    34.             sFriction.stiffness = hit.collider.material.staticFriction * originalSidewaysStiffness;
    35.             wheel.sidewaysFriction = sFriction;
    36.         }
    37.     }
    38. }
    39.  
     
  4. Greenwater79

    Greenwater79

    Joined:
    Nov 20, 2021
    Posts:
    8
    This is AWESOME. It works perfectly in Unity 2020.3.25. Add this script to the wheel colliders and then make a physics material and add that physics material to the objects you will drive on. Thanks SO much for this code!
     
    marclar83 and uniKorn8 like this.
  5. uniKorn8

    uniKorn8

    Joined:
    Feb 11, 2020
    Posts:
    1
    FINALLY!! Thank you so much for the solution.
     
    marclar83 likes this.