Hello, So I have this AI script that has different states in it. i have an audio source located on my character an i wold like the audio clip to change in every state. whats the approach to go about that? also, I have very limited scripting knowledge.
thanks in advance love you all
What’s important is what audio you want to play. You can use a general AudioSource to start playing a sound that is mixed with other sounds. Add more audio sources for sounds that are continuous and require start/stop playback.
public AudioClip clipFire; //set in inspector to a clip
AudioSource generalAS;
AudioSource engineAS;
void Start()
{
//this:
generalAS = GetComponent<AudioSource>();
//or this for multiple audio sources:
foreach (AudioSource aSource in GetComponents<AudioSource>())
{ //we have 2 audiosources
if (aSource.clip!=null && aSource.clip.name.Equals("engine"))
engineAS = aSource;
else
generalAS = aSource;
}
}
void Update()
{
if(bulletFire) generalAS.PlayOneShot(clipFire);
if(startEngine) engineAS.Play();
if(stopEngine) engineAS.Stop();
}