Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

sound repeating

Discussion in 'Scripting' started by spazy_t, May 10, 2007.

  1. spazy_t

    spazy_t

    Joined:
    Feb 24, 2007
    Posts:
    24
    Hello peeps.

    Right, i have an audioclip that plays when an animation is playing, which is triggered by a collision. Im using the !audio.isPlaying script so it on plays once while the animation is running. However, it then plays again once the animation finishes, i do not want this to happen. I;ve tried a few ways to get round it but no success.

    Any ideas?

    oh yeah here's the code so far:

    var lift : AudioClip;

    function Start(){
    animation.Stop("downLift");
    }

    function OnCollisionEnter (collision:Collision){
    if (collision.gameObject.tag=="oshi"){
    yield new WaitForSeconds (1);
    animation.Play("downLift");
    }
    if(!audio.isPlaying){
    audio.Play();
    }
    }



    Cheers.
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Wouldnt it work if you removed this line?

    f(!audio.isPlaying)

    You might want to PlayOneShot

    Not sure if this will solve your problem, but try it
    AC
     
  3. spazy_t

    spazy_t

    Joined:
    Feb 24, 2007
    Posts:
    24
    Nah, that dont work either. The sound is played when it collides with an object that acts as a lift down to a level. But because of the gravity, the character tends to jump up a bit every now and then, consiquently re-colliding with it causing the audio to be played again.

    Realy i could do with the sound to be told to play just the once, or for the collider to turn off some how, when the animation is running.
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I tend to do things in a strange kind of way, but what if you had another game object with the sound on it, and on collision, it becomes active ( have it Inactive on Awake). that way repeat collisions are going to make an object that is already active, active.

    Or, have another trigger zone that repesents your lift, but its bigger than just the floor (ie the size of the lift room itself) that way your going to stay in that area and not rectrigger everytime like it does now. You could look at OnTriggerStay for that...

    Im pretty sure the trigger functions in unity automatically work if theGO is tagged"Player", and that triggers will work better than collisions for your purposes...But thats one guys opinion.

    HTH
    AC
     
  5. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    maybe something like this? - not tested ; )
    (hold time should be long enough to cover your sound animation length)

    Code (csharp):
    1.  
    2. var lift : AudioClip;
    3. var holdTime = 3.0;
    4.  
    5. private var liftActive = false;
    6.  
    7. function Start()
    8. {
    9.     animation.Stop("downLift");
    10. }
    11.  
    12. function OnCollisionEnter (collision:Collision)
    13. {
    14.     if (collision.gameObject.tag=="oshi"  liftActive == false)
    15.     {
    16.         liftActive = true;
    17.         StartCoroutine ("Hold");
    18.         yield new WaitForSeconds (1);
    19.         animation.Play("downLift");
    20.         audio.Play();
    21.     }
    22. }
    23.  
    24. function Hold ()
    25. {
    26.     yield WaitForSeconds (holdTime);
    27.     liftActive = false;
    28. }
    EDIT: Cleaned up, make audio oneshot. May not be elegant but this should work.