Search Unity

Sometimes, Physics materials values need to be update at runtime to refresh material to make it work

Discussion in 'Physics' started by Karrix, Apr 4, 2021.

  1. Karrix

    Karrix

    Joined:
    Feb 10, 2019
    Posts:
    10
    Hello,
    I have a bug on Unity 2019.3.15f1 which appear sometimes.
    I have a physic material on my player which all values are zero and combines are average.
    I have another material with same values on wall.
    The goal is to make the player not stuck in wall when fall.
    It work but sometimes I have the bug which make the PLAYER material value are not applied at runtime then I have to change the values of the material and then back them (at runtime too) and then it work.
    It's not only friction bug, if I put 1 of bounciness, player not bounce while I not refresh material values (runtime).

    If I build the game with the bug, I always have this bug when playing.
    The bug not require specific condition, it appear sometimes on multiple project with this version of unity.

    If in editor I dupplicate the material and apply the copy to the player, it solve the bug but it will come back in future.
     
    Last edited: Apr 4, 2021
    ethanicus likes this.
  2. ethanicus

    ethanicus

    Joined:
    Aug 24, 2015
    Posts:
    40
    I can confirm this is happening in version 2020.3.34f1. My objects randomly started exhibiting different friction behavior, and editing the material in the editor while playing would fix it. Just as you said, duplicating the physics material and reapplying it solved the issue.
     
  3. Tarkick

    Tarkick

    Joined:
    Jan 18, 2019
    Posts:
    1
    Same issue on 2021.3.14f1. I almost gone mad trying to figure out where this was coming from.
     
  4. IvovdMarel

    IvovdMarel

    Joined:
    Jun 1, 2014
    Posts:
    9
    I ran into this bug in Unity 2021.3.21f1 and was able to 'solve' it by reapplying the material on Awake.


    Code (CSharp):
    1. public class PhysicMaterialUpdater : MonoBehaviour
    2. {
    3.     private Collider collider;
    4.  
    5.     // Start is called before the first frame update
    6.     void Awake()
    7.     {
    8.         collider = GetComponent<Collider>();
    9.         if (collider == null)
    10.         {
    11.             Debug.LogError($"No Collider component found on {name}.");
    12.             return;
    13.         }
    14.  
    15.         if (collider.sharedMaterial == null)
    16.         {
    17.             Debug.LogError($"No Physic Material found on {name}.");
    18.             return;
    19.         }
    20.  
    21.      
    22.        collider.material = new PhysicMaterial()
    23.        {
    24.            dynamicFriction = collider.sharedMaterial.dynamicFriction,
    25.            staticFriction = collider.sharedMaterial.staticFriction,
    26.            bounciness = collider.sharedMaterial.bounciness,
    27.            frictionCombine = collider.sharedMaterial.frictionCombine,
    28.            bounceCombine = collider.sharedMaterial.bounceCombine
    29.        };
    30.     }
    31. }
     
    Last edited: Mar 10, 2024