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

Translate walk sound script from JS to C#.

Discussion in 'Scripting' started by Harardin, Aug 31, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Hello I decided to translate JS to C# script. I just didn’t find one on C#, and for me C# is more understandable then JS.

    And I meet some problems with this. When FPS controller go forward it plays sound of walk one time, and plays it when FPS controller is stopped, but it didn’t play the sound when FPS controller is actually walking.

    Also I experimented with changing yield return null to StopCourutine in else statement but it didn’t work, sorry I just don’t have to much experience with courutines.

    Also I have some problem with translating this part of JS.
    Problem part in JS that I dont know how to translate maybe in this part all problem go.
    Code (JavaScript):
    1. function Start(){
    2.     var aSources = GetComponents(AudioSource);
    3.     footSource1 = aSources[0];
    4.     footSource2 = aSources[1];
    Also here is the full JS script of walking sound.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var stepWood : AudioClip[];
    4. var stepConcrete : AudioClip[]; // fill this array with the sounds at the Inspector
    5. var stepForest : AudioClip[];
    6. var footSource1 : AudioSource;
    7. var footSource2 : AudioSource;
    8. var delayBeforeSteps : float = 0.20;
    9. var delayBetweenSteps: float = 0.45;
    10. var audioStepLength : float = 0.60;
    11. var groundType : int;
    12. var isPlayerWalking : float = 0.0;
    13. var footAudioRandomness = 0.1;
    14. var soundEffectPitchRandomness = 0.05;
    15. function Update(){
    16.     //Check to see if the player is walking by checking for input from the walking keys
    17.     if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
    18.         //if those keys are pressed, the player must be walking...
    19.         isPlayerWalking += Time.deltaTime;
    20.     }
    21.     else{
    22.         //if those keys aren't pressed, the player can't be walking.....
    23.         isPlayerWalking = 0;
    24.     }
    25. }
    26. function Start(){
    27.     var aSources = GetComponents(AudioSource);
    28.     footSource1 = aSources[0];
    29.     footSource2 = aSources[1];
    30.     while(true){
    31.         if (isPlayerWalking >= 0.20 && groundType == 1){
    32.             yield WaitForSeconds(delayBeforeSteps);
    33.             footSource1.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
    34.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    35.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    36.             footSource1.Play();
    37.             if(isPlayerWalking == 0){
    38.                 yield;}
    39.             else{
    40.                 yield WaitForSeconds(delayBetweenSteps);
    41.                 footSource2.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
    42.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    43.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    44.                 footSource2.Play();
    45.                 yield WaitForSeconds(audioStepLength);
    46.                 if(isPlayerWalking == 0){
    47.                     yield;}
    48.             }
    49.            
    50.         }
    51.         else if ( isPlayerWalking >= 0.20 && groundType == 2){
    52.             yield WaitForSeconds(delayBeforeSteps);
    53.             footSource1.clip = stepWood[Random.Range(0, stepWood.length)];
    54.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    55.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    56.             footSource1.Play();
    57.             if(isPlayerWalking == 0){
    58.                 yield;}
    59.             else{
    60.                 yield WaitForSeconds(delayBetweenSteps);
    61.                 footSource2.clip = stepWood[Random.Range(0, stepWood.length)];
    62.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    63.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    64.                 footSource2.Play();
    65.                 yield WaitForSeconds(audioStepLength);
    66.                 if(isPlayerWalking == 0){
    67.                     yield;}
    68.             }
    69.         }
    70.         else if ( isPlayerWalking >= 0.20 && groundType == 3){
    71.             yield WaitForSeconds(delayBeforeSteps);
    72.             footSource1.clip = stepForest[Random.Range(0, stepForest.length)];
    73.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    74.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    75.             footSource1.Play();
    76.             if(isPlayerWalking == 0){
    77.                 yield;}
    78.             else{
    79.                 yield WaitForSeconds(delayBetweenSteps);
    80.                 footSource2.clip = stepForest[Random.Range(0, stepForest.length)];
    81.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    82.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    83.                 footSource2.Play();
    84.                 yield WaitForSeconds(audioStepLength);
    85.                 if(isPlayerWalking == 0){
    86.                     yield;}
    87.             }
    88.         }
    89.    
    90.         else{
    91.             yield;
    92.         }
    93.     }
    94. }
    95. function OnControllerColliderHit (hit : ControllerColliderHit){
    96.     if (hit.gameObject.tag == "concreteFloor"){
    97.         groundType = 1;
    98.        // print("Concrete");
    99.     }
    100.     else if (hit.gameObject.tag == "woodFloor"){
    101.         groundType = 2;
    102.        // print("Wood");
    103.     }
    104.     else if (hit.gameObject.tag == "forestFloor"){
    105.         groundType = 3;
    106.        // print("Forest");
    107.     }
    108. }
    And the one that I translated.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class footStepSoundControl : MonoBehaviour {
    5.     public AudioClip[] stepWood; // I am not so experienced with array so ask someone more exepirienced then me how to use it.
    6.     public AudioSource footSource1; // AudioSource is public so you can set up AudioSource in any place on your character.
    7.     public AudioSource footSource2;
    8.     private float delayBeforeSteps = 0.20f;   // All float can be public I just prefer privat. If you comfortable to set up them in Inspector just change to public.
    9.     private float delayBetweenSteps = 0.45f;
    10.     private float audioStepLength = 0.60f;
    11.     private float isPlayerWalking = 0.0f; // This one check speed of the player.
    12.     private int groundType;
    13.     private float footAudiRandomness = 0.1f;
    14.     private float soundEffectPitchRandomness = 0.05f; // this two gives your audio randomnes so its not going to sound always the same.
    15.     //then go Update
    16.     void Update ()
    17.     {
    18.         // and then go boolean check if player is walking or its not.
    19.         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    20.         {
    21.             isPlayerWalking += Time.deltaTime;
    22.             StartCoroutine(WalkSound());
    23.         }
    24.         else
    25.         {
    26.             isPlayerWalking = 0;
    27.             StopCoroutine(WalkSound());
    28.         }
    29.     }
    30.     IEnumerator WalkSound()
    31.     {
    32.         while (true)
    33.         {
    34.             if (isPlayerWalking >= 0.20f && groundType == 1)
    35.             {
    36.                 yield return new WaitForSeconds(delayBeforeSteps);
    37.                 footSource1.clip = stepWood[Random.Range(0, stepWood.Length)];
    38.                 footSource1.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
    39.                 footSource1.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness); // this states make sound play different all the time.
    40.                 footSource1.Play();
    41.                 if (isPlayerWalking == 0)
    42.                 {
    43.                     yield return null;
    44.                 }
    45.                 else
    46.                 {
    47.                     yield return new WaitForSeconds(delayBetweenSteps);
    48.                     footSource2.clip = stepWood[Random.Range(0, stepWood.Length)];
    49.                     footSource2.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
    50.                     footSource2.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness);
    51.                     footSource2.Play();
    52.                     yield return new WaitForSeconds(audioStepLength);
    53.                     if (isPlayerWalking == 0)
    54.                     {
    55.                         yield return null;
    56.                     }
    57.                 }
    58.  
    59.             }
    60.             else
    61.             {
    62.                 yield return null;
    63.             }
    64.             yield return null;
    65.         }
    66.     }
    67.     void OnControllerColliderHit(ControllerColliderHit hit)
    68.     {
    69.         if (hit.gameObject.tag == "woodFloor") // string must contain the name of the tag. make sure its set up right.
    70.         {
    71.             groundType = 1;
    72.         }
    73.     }
    74. }
    75.  
    Can some one help to finish it?
     
  2. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    I tryed to make this part of JS
    Code (CSharp):
    1. var aSources = GetComponents(AudioSource);
    2.     footSource1 = aSources[0];
    3.     footSource2 = aSources[1];
    Like this
    Code (CSharp):
    1. private AudioSource[] aSource;
    2.     //then go Update
    3.     void Update ()
    4.     {
    5.         // and then go boolean check if player is walking or its not.
    6.         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    7.         {
    8.             isPlayerWalking += Time.deltaTime;
    9.             StartCoroutine(WalkSound());
    10.         }
    11.         else
    12.         {
    13.             isPlayerWalking = 0;
    14.             StopCoroutine(WalkSound());
    15.         }
    16.     }
    17.     IEnumerator WalkSound()
    18.     {
    19.         footSource1 = aSource[0];
    20.         footSource2 = aSource[1];
    But its just give me this error
    NullReferenceException: Object reference not set to an instance of an object
    footStepSoundControl+<WalkSound>c__Iterator0.MoveNext () (at Assets/ScriptsTranslate/footStepSoundControl.cs:33)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    footStepSoundControl:Update() (at Assets/ScriptsTranslate/footStepSoundControl.cs:23)
     
  3. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Clean the error by setuping code like this. but sound still plays bad. Here some Audio record how it plays in test scene when Controller is walking.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class footStepSoundControl : MonoBehaviour {
    5.     public AudioClip[] stepWood; // I am not so experienced with array so ask someone more exepirienced then me how to use it.
    6.     public AudioSource footSource1; // AudioSource is public so you can set up AudioSource in any place on your character.
    7.     public AudioSource footSource2;
    8.     private float delayBeforeSteps = 0.20f;   // All float can be public I just prefer privat. If you comfortable to set up them in Inspector just change to public.
    9.     private float delayBetweenSteps = 0.45f;
    10.     private float audioStepLength = 0.60f;
    11.     private float isPlayerWalking = 0.0f; // This one check speed of the player.
    12.     private int groundType;
    13.     private float footAudiRandomness = 0.1f;
    14.     private float soundEffectPitchRandomness = 0.05f; // this two gives your audio randomnes so its not going to sound always the same.
    15.     private AudioSource[] aSource;
    16.     //then go Update
    17.     void Update ()
    18.     {
    19.         // and then go boolean check if player is walking or its not.
    20.         if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    21.         {
    22.             isPlayerWalking += Time.deltaTime;
    23.             StartCoroutine(WalkSound());
    24.         }
    25.         else
    26.         {
    27.             isPlayerWalking = 0;
    28.             StopCoroutine(WalkSound());
    29.         }
    30.     }
    31.     IEnumerator WalkSound()
    32.     {
    33.         aSource = GetComponents<AudioSource>();
    34.         footSource1 = aSource[0];
    35.         footSource2 = aSource[1];
    36.         while (true)
    37.         {
    38.             if (isPlayerWalking >= 0.20f && groundType == 1)
    39.             {
    40.                 yield return new WaitForSeconds(delayBeforeSteps);
    41.                 footSource1.clip = stepWood[Random.Range(0, stepWood.Length)];
    42.                 footSource1.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
    43.                 footSource1.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness); // this states make sound play different all the time.
    44.                 footSource1.Play();
    45.                 if (isPlayerWalking == 0)
    46.                 {
    47.                     yield return null;
    48.                 }
    49.                 else
    50.                 {
    51.                     yield return new WaitForSeconds(delayBetweenSteps);
    52.                     footSource2.clip = stepWood[Random.Range(0, stepWood.Length)];
    53.                     footSource2.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
    54.                     footSource2.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness);
    55.                     footSource2.Play();
    56.                     yield return new WaitForSeconds(audioStepLength);
    57.                     if (isPlayerWalking == 0)
    58.                     {
    59.                         yield return null;
    60.                     }
    61.                 }
    62.  
    63.             }
    64.             else
    65.             {
    66.                 yield return null;
    67.             }
    68.             yield return null;
    69.         }
    70.     }
    71.     void OnControllerColliderHit(ControllerColliderHit hit)
    72.     {
    73.         if (hit.gameObject.tag == "woodFloor") // string must contain the name of the tag. make sure its set up right.
    74.         {
    75.             groundType = 1;
    76.         }
    77.     }
    78. }
    79.  
     

    Attached Files:

  4. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    As someone who is in the process of implementing just the same system (albeit more sophisticated), here are some tips:
    1. Drop the whole coroutine logic, do it in Update()
    2. Don't use Time.deltaTime to determine how your player moved (because it has nothing to do with it)
    3. Use the actual distance the player traveled to determine when to play the next sound
    4. Don't use gameobject.tag == "whatever" but rather gameobject.CompareTag("whatever") as it's slightly more efficienct
    5. Don't randomize the volume and pitch, but rather use actually different sound files as there are more variations between stepsounds and randomizing these two factors doesn't do it justice
    6. For humanoid characters, I wouldn't use two AudioSources in different places to get the positional sound. Rather use two sources at the center, no spartial blending, falloff, etc. and instead rely on panning. It's good enough for such a subtle effect and should be dirt-cheap. Values of around 0.1-0.3 should be good. Be sure to keep it subtle.