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.
function Start(){
var aSources = GetComponents(AudioSource);
footSource1 = aSources[0];
footSource2 = aSources[1];
Also here is the full JS script of walking sound.
#pragma strict
var stepWood : AudioClip[];
var stepConcrete : AudioClip[]; // fill this array with the sounds at the Inspector
var stepForest : AudioClip[];
var footSource1 : AudioSource;
var footSource2 : AudioSource;
var delayBeforeSteps : float = 0.20;
var delayBetweenSteps: float = 0.45;
var audioStepLength : float = 0.60;
var groundType : int;
var isPlayerWalking : float = 0.0;
var footAudioRandomness = 0.1;
var soundEffectPitchRandomness = 0.05;
function Update(){
//Check to see if the player is walking by checking for input from the walking keys
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
//if those keys are pressed, the player must be walking...
isPlayerWalking += Time.deltaTime;
}
else{
//if those keys aren't pressed, the player can't be walking.....
isPlayerWalking = 0;
}
}
function Start(){
var aSources = GetComponents(AudioSource);
footSource1 = aSources[0];
footSource2 = aSources[1];
while(true){
if (isPlayerWalking >= 0.20 && groundType == 1){
yield WaitForSeconds(delayBeforeSteps);
footSource1.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource1.Play();
if(isPlayerWalking == 0){
yield;}
else{
yield WaitForSeconds(delayBetweenSteps);
footSource2.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource2.Play();
yield WaitForSeconds(audioStepLength);
if(isPlayerWalking == 0){
yield;}
}
}
else if ( isPlayerWalking >= 0.20 && groundType == 2){
yield WaitForSeconds(delayBeforeSteps);
footSource1.clip = stepWood[Random.Range(0, stepWood.length)];
footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource1.Play();
if(isPlayerWalking == 0){
yield;}
else{
yield WaitForSeconds(delayBetweenSteps);
footSource2.clip = stepWood[Random.Range(0, stepWood.length)];
footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource2.Play();
yield WaitForSeconds(audioStepLength);
if(isPlayerWalking == 0){
yield;}
}
}
else if ( isPlayerWalking >= 0.20 && groundType == 3){
yield WaitForSeconds(delayBeforeSteps);
footSource1.clip = stepForest[Random.Range(0, stepForest.length)];
footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource1.Play();
if(isPlayerWalking == 0){
yield;}
else{
yield WaitForSeconds(delayBetweenSteps);
footSource2.clip = stepForest[Random.Range(0, stepForest.length)];
footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
footSource2.Play();
yield WaitForSeconds(audioStepLength);
if(isPlayerWalking == 0){
yield;}
}
}
else{
yield;
}
}
}
function OnControllerColliderHit (hit : ControllerColliderHit){
if (hit.gameObject.tag == "concreteFloor"){
groundType = 1;
// print("Concrete");
}
else if (hit.gameObject.tag == "woodFloor"){
groundType = 2;
// print("Wood");
}
else if (hit.gameObject.tag == "forestFloor"){
groundType = 3;
// print("Forest");
}
}
And the one that I translated.
using UnityEngine;
using System.Collections;
public class footStepSoundControl : MonoBehaviour {
public AudioClip[] stepWood; // I am not so experienced with array so ask someone more exepirienced then me how to use it.
public AudioSource footSource1; // AudioSource is public so you can set up AudioSource in any place on your character.
public AudioSource footSource2;
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.
private float delayBetweenSteps = 0.45f;
private float audioStepLength = 0.60f;
private float isPlayerWalking = 0.0f; // This one check speed of the player.
private int groundType;
private float footAudiRandomness = 0.1f;
private float soundEffectPitchRandomness = 0.05f; // this two gives your audio randomnes so its not going to sound always the same.
//then go Update
void Update ()
{
// and then go boolean check if player is walking or its not.
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
isPlayerWalking += Time.deltaTime;
StartCoroutine(WalkSound());
}
else
{
isPlayerWalking = 0;
StopCoroutine(WalkSound());
}
}
IEnumerator WalkSound()
{
while (true)
{
if (isPlayerWalking >= 0.20f && groundType == 1)
{
yield return new WaitForSeconds(delayBeforeSteps);
footSource1.clip = stepWood[Random.Range(0, stepWood.Length)];
footSource1.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
footSource1.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness); // this states make sound play different all the time.
footSource1.Play();
if (isPlayerWalking == 0)
{
yield return null;
}
else
{
yield return new WaitForSeconds(delayBetweenSteps);
footSource2.clip = stepWood[Random.Range(0, stepWood.Length)];
footSource2.volume = Random.Range(0.4f - footAudiRandomness, 0.4f + footAudiRandomness);
footSource2.pitch = Random.Range(1.0f - soundEffectPitchRandomness, 1.0f + soundEffectPitchRandomness);
footSource2.Play();
yield return new WaitForSeconds(audioStepLength);
if (isPlayerWalking == 0)
{
yield return null;
}
}
}
else
{
yield return null;
}
yield return null;
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "woodFloor") // string must contain the name of the tag. make sure its set up right.
{
groundType = 1;
}
}
}
Can some one help to finish it?