Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Foot Steps on Physics materials not working properly in List

Discussion in 'Scripting' started by KenClemson, Dec 6, 2016.

  1. KenClemson

    KenClemson

    Joined:
    Oct 6, 2016
    Posts:
    24
    Hi Im a bit new to List's and I was trying to store my physicsmaterials in a List for my footstep sounds. When I walk on the physics material it does trigger but only ever triggers the first material in the List and wont trigger the other ones.
    Code (CSharp):
    1.     public List<PhysicMaterial> footStepPhysicsMaterials = new List<PhysicMaterial>();
    2.  
    3.     void OnCollisionEnter(Collision hit)
    4.     {
    5.         //groundType = 0;
    6.  
    7.         //Debug.Log("Sim is walking in no defined mateial");
    8.  
    9.         if (hit.collider.sharedMaterial == footStepPhysicsMaterials[0])
    10.         {
    11.             groundType = 1;
    12.             //Debug.Log("Sim is walking in gravel physics material");
    13.         }
    14.  
    15.         if (hit.collider.sharedMaterial == footStepPhysicsMaterials[1])
    16.         {
    17.             groundType = 2;
    18.             //Debug.Log("Sim is walking on physics metal");
    19.         }
    20.         if (hit.collider.sharedMaterial == footStepPhysicsMaterials[2])
    21.         {
    22.             groundType = 3;
    23.             //Debug.Log("Sim is walking on physics Concrete");
    24.         }
    25.         if (hit.collider.sharedMaterial == footStepPhysicsMaterials[3])
    26.         {
    27.             groundType = 4;
    28.             //Debug.Log("Sim is walking on physics grass");
    29.         }
    30.  
    31.     }
     
  2. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    When in doubt, Debug !
    Code (csharp):
    1. Debug.Log( gameObject.name + " has collided with " hit.collider.gameObject.name );
    2. Debug.Log( hit.collider.gameObject.name + " has physics material " hit.collider.sharedMaterial.name );
    Also consider using a loop :
    Code (csharp):
    1. groundType = -1;
    2.  
    3. for ( int i = 0; i < footStepPhysicsMaterials.Length; i++ )
    4.    if ( hit.collider.sharedMaterial == footStepPhysicsMaterials[i] )
    5.      groundType = i;
    6.  
    7. if ( groundType == -1 ) Debug.LogWarning( "no match found for " hit.collider.sharedMaterial.name );
    8. else Debug.Log( "Sim is walking on physics " + footStepPhysicsMaterials[groundType].name );