Search Unity

Showcase Evaluate my step sound script

Discussion in 'Scripting' started by Fitbie, Mar 29, 2023.

  1. Fitbie

    Fitbie

    Joined:
    Aug 17, 2021
    Posts:
    66
    Hi!
    It's not a question or a problem, I just need an outside perspective on whether I'm doing the right thing.
    So, I created two scripts for the character steps. In both, I call PlayStepsSound() from the animation event. In both cases on each scene I have a script that sets the groundType(enum) of steps for that scene (outside ground, indoors wood, etc.), in both cases I have a set of lists, containing the step sounds from which I randomly play.

    In the first case I go the easy way and make a script similar to the one in the Unity demo. What I didn't like about it: every time we call PlayStepsSound() we check the type of steps. In my game, there is one type of step in every scene.
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerStepsSound : MonoBehaviour
    6. {
    7.     [SerializeField] private AudioSource stepsAudio;
    8.     [SerializeField] private List<AudioClip> groundClips = new List<AudioClip>();
    9.     [SerializeField] private List<AudioClip> woodenFloorClips = new List<AudioClip>();
    10.     [SerializeField] private List<AudioClip> groundMareClips = new List<AudioClip>();
    11.    
    12.  
    13.     public enum GroundType
    14.     {
    15.         Ground,
    16.         WoodenFloor,
    17.         GroundMare,
    18.     }
    19.  
    20.     public GroundType groundType;
    21.  
    22.     public void PlayStepsSound()
    23.     {
    24.         if(groundType == GroundType.Ground)
    25.             stepsAudio.PlayOneShot(groundClips[Random.Range(0, groundClips.Count)]);
    26.         else if(groundType == GroundType.WoodenFloor)
    27.             stepsAudio.PlayOneShot(woodenFloorClips[Random.Range(0, woodenFloorClips.Count)]);
    28.         else if(groundType == GroundType.GroundMare)
    29.             stepsAudio.PlayOneShot(groundMareClips[Random.Range(0, groundMareClips.Count)]);
    30.  
    31.     }
    32.  

    In the second case, I use a setter for enum to change the steps type not every time we call it, but only when it actually changes. I also use a void delegate to store the current step method, and in PlayStepSound() I simply invoke this delegate.
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerStepsSound : MonoBehaviour
    6. {
    7.     [SerializeField] private AudioSource stepsAudio;
    8.     [SerializeField] private List<AudioClip> groundClips = new List<AudioClip>();
    9.     [SerializeField] private List<AudioClip> woodenFloorClips = new List<AudioClip>();
    10.     [SerializeField] private List<AudioClip> groundMareClips = new List<AudioClip>();
    11.    
    12.     private delegate void StepsSoundMethod();
    13.     private StepsSoundMethod stepsSoundMethod;
    14.  
    15.     public enum GroundType
    16.     {
    17.         Ground,
    18.         WoodenFloor,
    19.         GroundMare,
    20.     }
    21.     private GroundType _groundType;
    22.     public GroundType groundType
    23.     {
    24.         get
    25.         {
    26.             return _groundType;
    27.         }
    28.         set
    29.         {
    30.             _groundType = value;
    31.             if(_groundType == GroundType.Ground)
    32.                 stepsSoundMethod = StepsGround;
    33.             else if (_groundType == GroundType.WoodenFloor)
    34.                 stepsSoundMethod = StepsWoodenFloor;
    35.             else if (_groundType == GroundType.GroundMare)
    36.                 stepsSoundMethod = StepsGroundMare;
    37.         }
    38.     }
    39.  
    40.     public void PlayStepsSound()
    41.     {
    42.         if(stepsSoundMethod != null)
    43.         {
    44.             stepsSoundMethod();
    45.         }
    46.     }
    47.    
    48.     private void StepsGround()
    49.     {
    50.         stepsAudio.PlayOneShot(groundClips[Random.Range(0, groundClips.Count)]);
    51.     }
    52.     private void StepsWoodenFloor()
    53.     {
    54.         stepsAudio.PlayOneShot(woodenFloorClips[Random.Range(0, woodenFloorClips.Count)]);
    55.     }
    56.     private void StepsGroundMare()
    57.     {
    58.         stepsAudio.PlayOneShot(groundMareClips[Random.Range(0, groundMareClips.Count)]);
    59.     }
    60. }
    61.  

    So, since I haven't learned how to profile code yet - can you tell me if I'm doing it right? I understand that maybe the second way is more complicated than necessary, but I am self-taught and like to learn things by trial and error.
    Thanks!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Seems pretty unneccesary, why don't you just have a reference to which List you want to access?

    Code (CSharp):
    1. List<AudioClip> playTheseClips;
    2. public void SetGroundType(GroundType newGroundType)
    3. {
    4.   if (newGroundType == GroundType.Ground)
    5.     playTheseClips = groundClips;
    6.   // etc.
    7. }
    8.  
    9. public void PlayStepsSound()
    10. {
    11.   stepsAudio.PlayOneShot(playTheseClips[Random.Range(0, groundClips.Count)]);
    12. }
     
    Fitbie likes this.