Hello there,
i’m testing some C# scripts in Unity and in this case i’m trying to get a “Ground Detector” wich is a simple box collider parented to each player’s foot, able to get tags from game objects (triggered) and play a different sounds depending on it.
I’m using the locomotor system, so with this “detector” i can obtain an effective ground detection, depending on the real foots/ground contact. Every foot get a ground tag independently, and send a command to the main Audio Source, wich play the right sound.
In some cases foots are on different surfaces (for ex: one on grass, one on concrete), and my Audio Source play the corresponding audio file correctly.
So, till this point the script works well, but sounds are a bit confusing because sometimes they’re played more than one at time. Also, when running, audio seem delayed.
I think one of the problem is about multiple ground collision on each step, with produce multiple detections, and in the end multiple sound played for each step.
How to solve that?
I’m also concerned about calculations (is this the reason that cause audio delaying?)…is my method optimized?
Here’s the script
using UnityEngine;
using System.Collections;
public class SoundEffectController : MonoBehaviour {
public AudioSource footAudioSource;
public AudioClip[] cementsteps;
public AudioClip[] grasssteps;
public AudioClip[] dirtsteps;
void OnTriggerEnter(Collider col){
if (col.tag == "pietra")
{
footAudioSource.PlayOneShot(cementsteps[Random.Range(0, cementsteps.Length)]);
}
else if(col.tag == "erba")
{
footAudioSource.PlayOneShot(grasssteps[Random.Range(0, grasssteps.Length)]);
}
else if(col.tag == "terra")
{
footAudioSource.PlayOneShot(dirtsteps[Random.Range(0, dirtsteps.Length)]);
}
}
}
My knowledge is limited for now, and i’m trying to understand scripts all around the web, mixing the useful parts for my purposes, so my tests are full of errors or illogical methods.
I’m new here, on UnityAnswer and in Unity coding and english is not my mothertongue, so be patient with me if you can :D.
I hope I explained it clearly.
Cheers.