- Home /
This question was
closed Apr 04, 2021 at 07:07 PM by
Demon1k for the following reason:
Problem is not reproducible or outdated
Play sound multiple times upon collision
I have this code :
public class PlaySound : MonoBehaviour
{
public AudioClip AnimationSound;
public float Volume = 0.5f;
public bool is2D;
[SaveableField, HideInInspector]
public bool isPlayed;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && !isPlayed)
{
if (AnimationSound)
{
if (!is2D)
{
AudioSource.PlayClipAtPoint(AnimationSound, transform.position, Volume);
}
else
{
Tools.PlayOneShot2D(transform.position, AnimationSound, Volume);
}
}
isPlayed = true;
}
}
}
how to make sound play 2 or more times so i can choose the number of repetitions
Thanks!
Comment
Answer by $$anonymous$$ · Apr 03, 2021 at 10:01 PM
Here you go public class PlaySound : MonoBehaviour {
public AudioClip AnimationSound;
public float Volume = 0.5f;
public bool is2D;
// replength is how many times to repeat the audio
// countlength is to understand the for loop (for you)
Public int repLength, countLength = 0;
[SaveableField, HideInInspector]
public bool isPlayed;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && !isPlayed)
{
For(countLength ; countLength = replength; countLength ++)
{
if (isPlayed = false)
{
if (!is2D)
{
AudioSource.PlayClipAtPoint(AnimationSound, transform.position, Volume);
}
else
{
Tools.PlayOneShot2D(transform.position, AnimationSound, Volume);
}
}
IsPlayed = true;
If (countLength > replength)
{
Countlength = 0;
}
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My EnemyAI is flying towards me 1 Answer
cancle invokeReapeating not working 0 Answers
Unity UFO tutorial question about code 0 Answers