Search Unity

trying to make a Sound Trigger

Discussion in 'Scripting' started by KenClemson, Nov 15, 2016.

  1. KenClemson

    KenClemson

    Joined:
    Oct 6, 2016
    Posts:
    24
    Hi Im hoping someone could help me work out how to bring up the fader from one end of a trigger from 0 at the edge of the trigger to 100 percent by the time you get to the middle then back down to 0 when you walk out the other side. I have drawn a diagram to try to explain what I mean. 20161115_174251.jpg
     
  2. KenClemson

    KenClemson

    Joined:
    Oct 6, 2016
    Posts:
    24
    This is what I have so far, sorry I'm not the best coder.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Audio;
    4.  
    5. public class AudioTrigger : MonoBehaviour
    6. {
    7.     //Audio stuff
    8.     public AudioMixer mixer;
    9.     public AudioMixerSnapshot[] snapshots;
    10.  
    11.     //Box Collider stuff
    12.     public Collider audioTriggerBox;
    13.     public Bounds bounds;
    14.  
    15.     private Transform centreOfBounds;
    16.     private Transform edgeOfBounds;
    17.     private float[] weights;
    18.     private Transform Player;
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         //find the centre of the collider
    24.         bounds = audioTriggerBox.bounds;
    25.         centreOfBounds.position = bounds.center;
    26.         edgeOfBounds.position = bounds.extents;
    27.  
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update ()
    32.     {
    33.         weights[0] = 0.0f;  //at the edge of the Collider
    34.      
    35.         weights[100] = 1.0f; //In the middle of the Collider
    36.  
    37.         mixer.TransitionToSnapshots(snapshots, weights , 1.0f);
    38.         //at the centre of the Bounding box
    39.    
    40.     }
    41. }
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
unityunity