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

Footstep system [is there a better way to do this]

Discussion in 'Scripting' started by PvTGreg, Jun 3, 2015.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Hi ive kinda gone into this blind and was trying to create a foot step system for my survival game kit and i was not sure if there is any better way to be doing this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FootStepManager : MonoBehaviour {
    5.  
    6.     public Grass grass = new Grass();
    7.     public Rock rock = new Rock ();
    8.     public Metal metal = new Metal();
    9.     public Snow snow = new Snow();
    10.  
    11.     void Update(){
    12.         RaycastHit hit;
    13.         if (transform.GetComponent<CharacterControlls> ().grounded == true) {
    14.             if (Physics.Raycast (transform.position, -Vector3.up, out hit)) {
    15.                 if (hit.transform.gameObject.GetComponent<MeshRenderer> ()) {
    16.                     Texture tex = hit.transform.gameObject.GetComponent<MeshRenderer> ().material.mainTexture;
    17.                     Debug.Log (tex.name);
    18.                     foreach (Texture textureG in grass.textures) {
    19.                         if (tex == textureG) {
    20.                             Debug.Log ("Grass");
    21.                         }
    22.                     }
    23.                     foreach (Texture textureR in rock.textures) {
    24.                         if (tex == textureR) {
    25.                             Debug.Log ("Rock");
    26.                         }
    27.                     }
    28.                     foreach (Texture textureM in metal.textures) {
    29.                         if (tex == textureM) {
    30.                             Debug.Log ("Metal");
    31.                         }
    32.                     }
    33.                     foreach (Texture textureS in snow.textures) {
    34.                         if (tex == textureS) {
    35.                             Debug.Log ("Snow");
    36.                         }
    37.                     }
    38.                 }
    39.             }
    40.         }
    41.     }
    42. }
    43. [System.Serializable]
    44. public class Grass{
    45.     public Texture[] textures;
    46. }
    47. [System.Serializable]
    48. public class Rock{
    49.     public Texture[] textures;
    50. }
    51. [System.Serializable]
    52. public class Metal{
    53.     public Texture[] textures;
    54. }
    55. [System.Serializable]
    56. public class Snow{
    57.     public Texture[] textures;
    58. }
     
  2. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    anyone got any input to this?
     
  3. Deleted User

    Deleted User

    Guest

    Well for one, all your classes (Metal, Rock, Snow, etc.) are the same which seems rather pointless. You should make it only 1 class and give them a name property.
     
  4. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    the reason i do this is so that it looks nice in the inspector
     
  5. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    ahh sorry i didnt read your comment properly thanks now with less

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FootStepManager : MonoBehaviour {
    5.     public GroundType groundType;
    6.     public Variables Grass = new Variables();
    7.     public Variables Rock = new Variables();
    8.     public Variables Sand = new Variables();
    9.     public Variables Snow = new Variables();
    10.     public Variables Metal = new Variables();
    11.     public enum GroundType
    12.     {
    13.         Grass,
    14.         Rock,
    15.         Metal,
    16.         Snow
    17.     }
    18.     void Update(){
    19.         RaycastHit hit;
    20.         if (transform.GetComponent<CharacterControlls> ().grounded == true) {
    21.             if (Physics.Raycast (transform.position, -Vector3.up , out hit)) {
    22.                 if (hit.transform.gameObject.GetComponent<MeshRenderer> ()) {
    23.                     Texture tex = hit.transform.gameObject.GetComponent<MeshRenderer> ().material.mainTexture;
    24.                     if (tex.name.Contains("Grass")) {
    25.                         groundType = GroundType.Grass;
    26.                     }
    27.                     if (tex.name.Contains("Rock")) {
    28.                         groundType = GroundType.Rock;
    29.                     }
    30.                     if (tex.name.Contains("Metal")) {
    31.                         groundType = GroundType.Metal;
    32.                     }
    33.                     if (tex.name.Contains("Snow")) {
    34.                         groundType = GroundType.Snow;
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
    41. [System.Serializable]
    42. public class Variables{
    43.     public Texture[] textures;
    44.     public AudioClip[] sounds;
    45. }
     
  6. Unity_Wolfpack

    Unity_Wolfpack

    Joined:
    Jun 7, 2015
    Posts:
    9
    is there a way to make the enums in the inspector.
    Tell it how many is in the list then set the Enum GroundType, textures, sounds.
    Kinda like in UFPS thanks for your help
     
  7. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    just add an
    [System.Serializable]
    tag before the enums like:

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public enum Test{
    4.   showInEditor,
    5.   thisTest
    6. }
     
  8. Unity_Wolfpack

    Unity_Wolfpack

    Joined:
    Jun 7, 2015
    Posts:
    9
    i had something like this going on but am having trouble
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FootStepEnum : MonoBehaviour {
    5.     private string groundName;
    6.  
    7.     public GroundTypeVariables[] VarType;
    8.  
    9.     public AudioSource audio;
    10.  
    11.  
    12.     void Update(){
    13.         RaycastHit hit;
    14.         int index;
    15.  
    16.         if (transform.GetComponent<CharMove> ().onGround == true) {
    17.             if (Physics.Raycast (transform.position, -Vector3.up , out hit)) {
    18.                 if (hit.transform.gameObject.GetComponent<MeshRenderer> ()) {
    19.                     Texture tex = hit.transform.gameObject.GetComponent<MeshRenderer> ().material.mainTexture;
    20.                     //find the texture from raycast hit and compare to list
    21.                     groundName = tex.name;
    22.  
    23.                     //
    24.                     //comare the groundName to the textures name in the VarType the get number save as int
    25.                     //
    26.  
    27.                     if (audio.isPlaying)
    28.                         return; // don't play a new sound while the last hasn't finished
    29.  
    30.                     //line below has some errors also
    31.                     //audio.clip = VarType[index].sounds[Random.Range(0,VarType[index].sounds.GetLength)];
    32.                     audio.Play();
    33.                 }
    34.             }
    35.         }
    36.     }
    37.  
    38. }
    39.  
    40. [System.Serializable]
    41. public class GroundTypeVariables{
    42.     public string nameID;
    43.     public Texture[] textures;
    44.     public AudioClip[] sounds;
    45. }
     
  9. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    what you want is a struct try this:
    Code (CSharp):
    1. public class FootStepEnum : MonoBehaviour {
    2.  
    3.  public GroundTypeVariables[] myDataInEditor;
    4.   [System.Serializable]
    5.   public struct GroundTypeVariables{
    6.     public string nameId;
    7.     public int someOtherData;
    8.   }
    9. }
     
  10. Unity_Wolfpack

    Unity_Wolfpack

    Joined:
    Jun 7, 2015
    Posts:
    9
    Thanks got it working now
     
  11. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    A couple things stand out as silly or incorrect:

    1) Use Array.IndexOf() rather than using a foreach loop and manually searching yourself.

    2) Calling .GetComponent<MeshRenderer>() twice is just silly.

    3) -Vector3.up when Vector3.down exists
     
  12. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    1.no longer using loops but thanks i could use this is some of my other code.
    2. the first call is to check if it has one before doing anything with it this was i don't get nulls
    3. so used to using that.i forgot that existed thanks
     
  13. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    im going to close this off for now as i have gotten alot of replys