Hello. I think this may be my first time posting here so I apologize if I violate any forum conventions.
I am working on a simple game in which you feed a cat different food items which results in the AudioSource representing the cat playing 1 of 4 AudioClips.
The player needs to have the ability to feed the cat more than one food item within the length of one AudioClip. I would explain why but it’s not really relevant.
The problem is that this is resulting in the second AudioClip starting before the first AudioClip has finished. It comes out sounding like more than one AudioClip overlapping.
I would like the first AudioClip to finish playing before a second AudioClip can be activated, but I don’t want them to stack in a queue.
This is the script I have at the moment. It is attached to the food object that you click.
// Update is called once per frame
void Update () {
//This is a timer before the food object starts moving across the screen
if (timer > 0) {
timer--;
//once the timer is done, the food starts moving along the Y axis of the screen
} else {
nextPosition.Set(this.transform.position.x , this.transform.position.y + foodSpeed , this.transform.position.z);
this.transform.SetPositionAndRotation (nextPosition, this.transform.rotation);
//if the food makes it to the end of the screen, it is moved back to its starting point
if (this.transform.position.y > endLine.y) {
this.transform.SetPositionAndRotation (startPoint, this.transform.rotation);
timer = foodDelay;
}
}
//if the screen is touched, use the eat food method <--This is where things start getting messy
if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began)) {
eatFood ();
}
}
//This method checks to see if you hit the food and then resets the foods position
//and also runs the play sound method. <--Probably where the bigger problem is.
void eatFood() {
//checks to a tap. Did I mention that this is an Android game?
Ray raycast = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
RaycastHit raycastHit;
//Ok I guess this is really where the checking happens
if (Physics.Raycast (raycast, out raycastHit)) {
//These two lines reset the food
this.transform.SetPositionAndRotation (startPoint, this.transform.rotation);
timer = foodDelay;
//And here is the thing that is probably causing the problems
playSound ();
}
}
//this plays a portion of an audio clip.
void playSound () {
//The sound is choosen at random. I only now realize how stupid it is to
//convert the random value to an integer-ish value
randomNumber = Random.value * 4;
/*
* This line of code was my easy fix solution but even it wouldn't work.
* This idea was to stop the old AudioClip before starting a new one.
* Unfortunately Unity didn't like me calling audioSource.Stop from
* a method.
audioSource.Stop;
*/
if (randomNumber < 1f) {
audioSource.PlayOneShot (nomming1, 1f);
} else if ((randomNumber > 1f) && (randomNumber < 2f)) {
audioSource.PlayOneShot (nomming2, 1f);
} else if ((randomNumber >= 2f) && (randomNumber < 3f)) {
audioSource.PlayOneShot (nomming3, 1f);
} else if (randomNumber >= 3f) {
audioSource.PlayOneShot (nomming4, 1f);
}
Debug.Log ("Playing audio");
}
This seems like a problem that a lot of people would probably come across but I haven’t found anything addressing it which makes me think that I may be missing something obvious. Can someone point me in the right direction?
-Thanks