Search Unity

Random Sound When Click?

Discussion in 'Audio & Video' started by charaypro, Dec 27, 2014.

  1. charaypro

    charaypro

    Joined:
    Nov 25, 2013
    Posts:
    3
    Hi Unity Community iam making a game in unity 3d and playing around with some sounds. what iam trying todo its make a ramdom sound when click or move like in RTS or Moba iam more with modelling and sounds creations but weak with code my friend gave me this script but only works with one sound like move only playing one sound here its my question.
    How do i make this script to work with select move or attack have a random sounds

    thanks in advance enjoy the holiday
    best regard
    charay




    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    using RUnit;

    namespace RUnit {

    [RequireComponent (typeof (Unit))]
    public class UnitAudio : MonoBehaviour {


    public AudioClip selectSound;
    public AudioClip moveSound;
    public AudioClip EngineSound;
    public AudioClip attackSound;
    public AudioClip hitSound;
    public AudioClip destroySound;

    //~ private AudioSource audioSrc;

    // Use this for initialization
    void Awake () {
    //~ audioSrc=gameObject.GetComponent<AudioSource>();
    //~ if(audioSrc==null){
    //~ audioSrc=gameObject.AddComponent<AudioSource>();
    //~ audioSrc.playOnAwake=false;
    //~ audioSrc.loop=false;
    //~ }

    Unit unit=gameObject.GetComponent<Unit>();
    if(unit!=null) unit.SetAudio(this);
    //else DestroyImmediate(this);
    }


    public void Select(){
    if(selectSound!=null)AudioManager.PlaySound(selectSound);}

    public void Move(){
    if(moveSound!=null)AudioManager.PlaySound(moveSound); AudioManager.PlaySound(EngineSound); }


    public void Attack(){
    if(attackSound!=null)AudioManager.PlaySound(attackSound); }

    public void Hit(){
    if(hitSound!=null)AudioManager.PlaySound(hitSound); }


    public float Destroy(){

    if(destroySound!=null){
    AudioManager.PlaySound(destroySound);
    return destroySound.length;
    }
    return 0;
    }

    }

    }
     
  2. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    Just create an array filled with audioclips and choose a random one.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. using RUnit;
    7.  
    8. namespace RUnit {
    9.  
    10. [RequireComponent (typeof (Unit))]
    11. public class UnitAudio : MonoBehaviour {
    12.  
    13.  
    14. public AudioClip selectSound;
    15. public AudioClip moveSound;
    16. public AudioClip EngineSound;
    17. public AudioClip attackSound;
    18. public AudioClip hitSound;
    19. public AudioClip destroySound;
    20.  
    21. public AudioClip[] clips; // new
    22.  
    23. //~ private AudioSource audioSrc;
    24.  
    25. // Use this for initialization
    26. void Awake () {
    27. //~ audioSrc=gameObject.GetComponent<AudioSource>();
    28. //~ if(audioSrc==null){
    29. //~ audioSrc=gameObject.AddComponent<AudioSource>();
    30. //~ audioSrc.playOnAwake=false;
    31. //~ audioSrc.loop=false;
    32. //~ }
    33.  
    34. Unit unit=gameObject.GetComponent<Unit>();
    35. if(unit!=null) unit.SetAudio(this);
    36. //else DestroyImmediate(this);
    37. }
    38.  
    39.  
    40. public void Select(){
    41. if(selectSound!=null)AudioManager.PlaySound(selectSound);}
    42.  
    43. public void Move(){
    44. if(moveSound!=null)AudioManager.PlaySound(moveSound); AudioManager.PlaySound(EngineSound); }
    45.  
    46.  
    47. public void Attack(){
    48. if(attackSound!=null)AudioManager.PlaySound(attackSound); }
    49.  
    50. public void Hit(){
    51. if(hitSound!=null)AudioManager.PlaySound(hitSound); }
    52.  
    53. // new
    54. public void RandomPlayClip()
    55. {
    56. AudioManager.PlaySound(clips[Random.Range(0, clips.length)]);
    57. }
    58.  
    59. public float Destroy(){
    60.  
    61. if(destroySound!=null){
    62. AudioManager.PlaySound(destroySound);
    63. return destroySound.length;
    64. }
    65. return 0;
    66. }
    67.  
    68. }
    69.  
    70. }
    71.  
    You can fill clips array from the inspector. I hope this helps.
     
    charaypro likes this.
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Yes, you can do that with the code above but when you want to make it *weighted* randomness you have to go altering the array and adding / removing elements. Cumbersome. Like if you wanted selectSound to play twice as often as the others from a random pool.

    You could just pick up our Master Audio plugin which does this and every other conceivable thing you'd need without you having to write any code. It's only $20 for a few more days - normally $75, and it's super optimized and runs on all platforms.

    Is there a reason you want to do it yourself? We've already implemented everything for your needs and we maintain the code and expand on it so that you don't have to worry about that and can spend your time on other problems.

    Good luck either way.
    -Brian
     
    charaypro likes this.
  4. charaypro

    charaypro

    Joined:
    Nov 25, 2013
    Posts:
    3
    hi Brian i pick up the master audio plugin and play with it the thing its that the plug in and this script dont work with ech other. in master audio plug in i make a group sound with random audio but i try to put the gameobject in my script but it will not work its say this need to be and audio source what will u recommended to do becouse this audioscript i have here its being call from another unitscript so it activated the sound like select and movement for the different unit i have in game thx agian for the reply iam still new with scripting but keep fighting my way to completion of this game
     
  5. charaypro

    charaypro

    Joined:
    Nov 25, 2013
    Posts:
    3
    AudioManager.PlaySound(clips[Random.Range(0, clips.length)]);
    }

    public float Destroy(){

    if(destroySound!=null){
    AudioManager.PlaySound(destroySound);
    return destroySound.length;
    }
    return 0;
    }

    }

    }
    [/CODE]

    You can fill clips array from the inspector. I hope this helps.[/QUOTE]

    hi zehreken
    iam been trying too understand this scripting world and fail totally :p but i keep fighting until it works..
    the script i have here its giving me alot of errors when i wont to make and arrays out of the script
    public AudioClip [] selectSound;
    public AudioClip [] moveSound;
    public AudioClip [] attackSound;
    public AudioClip [] hitSound;
    public AudioClip [] destroySound;

    public void Select()
    {
    if(selectSound!=null)AudioManager.PlaySound(selectSound[Random.Range(0, clips.length)]);
    }

    i think the error comes from another script that in use to call this audio script its working like this i have this one script for multiply units lets say its rts game so the unit script calling this audio script for audio so i change the audio in this script for different units so that why i try to make the public AudioClip [] selectSound; but fail the script giving me and error and i need to put everthing back to audioclip as they was from the begining iam new with scripting i hope u understand but the underline its that iam crying alot when iam working with script... and i try to buy the masteraudio plug in and that one dont works with this script :/ damn but thanks for the reply iam so noob for the scripting world so i dont see what too do when the answar its right in front of me thanks agian for your reply

    zehreken
     
  6. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I'm not sure I understand, but please watch our short Youtube videos and then contact us again if you need assistance.