Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Noob needs help with C# - Play audio on trigger enter.

Discussion in 'Scripting' started by Christo_Katkos, Oct 17, 2014.

  1. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    Ok so I have my health system setup with health pickups, health counter and a loadlevel if the player dies.

    So now I want an Hurt audio clip to play once when the player enters the "Trap" trigger.
    Problem is, I dont know what code to add to the script to get the audio file and then play it if the player triggers the trap.

    Here is the code.
    Code (CSharp):
    1.     private HealthCounter healthCounter;
    2.    
    3.     public int playerStartingHealth = 5;
    4.     public int playerCurrentHealth;
    5.     public AudioClip hurt;
    6.    
    7.     // Use this for initialization
    8.     void Awake ()
    9.     {
    10.         playerCurrentHealth = playerStartingHealth;
    11.         healthCounter = GameObject.Find("HealthCounterText").GetComponent<HealthCounter>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         healthCounter.healthLeft = playerCurrentHealth;
    18.        
    19.         if(playerCurrentHealth == 0)
    20.         {
    21.             Application.LoadLevel(0);
    22.         }
    23.        
    24.     }
    25.     void OnTriggerEnter(Collider other)
    26.     {
    27.         if (other.gameObject.tag == "Trap")
    28.         {
    29.             playerCurrentHealth --;
    30.             //AUDIO NEEDS TO PLAY HERE, BUT HOW?
    31.         }
    32.  
    33.         if (other.gameObject.tag == "HealthPickup")
    34.         {
    35.             playerCurrentHealth ++;
    36.         }
    37.     }
    38.            
    39. }
     
  2. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    Hey buddy.
    You can add this line of code wherever you want to play the audio.
    Code (CSharp):
    1. AudioSource.PlayClipAtPoint(clip, transform.position);
    this is the documentation link
    http://docs.unity3d.com/ScriptReference/AudioSource.PlayClipAtPoint.html
    * make sure before you use the audio clip, change it to 2D clip in the import setting window at the right side if you don't need to play the sound in specific area.

    This works really good because you don't have to add audiosource component in every gameobject that you need sfx.
     
    Christo_Katkos likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    If its just a single sound on that trap (and you want to keep 3D sound),
    its also simple to call something similar to:

    Code (CSharp):
    1.  if (other.gameObject.tag == "Trap")
    2.         {
    3.             playerCurrentHealth --;
    4.             //AUDIO NEEDS TO PLAY HERE, BUT HOW?
    5.             other.audio.Play();  
    6.  
    7.         }
     
  4. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    That did the job, spawns the sound and destroys if afterwards.
    Thanks a milli.
     
    DylanYasen likes this.
  5. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    No problem brother.
    Good luck developing