Want more features on Pastebin? Sign Up, it's FREE!
Guest

Sound Timer

By: a guest on Jun 11th, 2013  |  syntax: C#  |  size: 1.44 KB  |  views: 49  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using UnityEngine;
  3.  
  4. public class ouyatest : MonoBehaviour
  5. {
  6.     public UILabel lblAxis1;
  7.     public UILabel lblAxis2;
  8.     public AudioSource BeepSource = null;
  9.     private DateTime m_timerSound = DateTime.MinValue;
  10.     private const float SOUND_TIME = 200;
  11.  
  12.         // Use this for initialization
  13.         void Start()
  14.         {
  15.         Input.ResetInputAxes();
  16.         OuyaExampleCommon.Player = OuyaSDK.OuyaPlayer.player1;
  17.         }
  18.        
  19.         // Update is called once per frame
  20.         void Update()
  21.         {
  22.         float joy1axis1 =
  23.             Input.GetAxisRaw(string.Format("Joy{0} Axis 1", (int)OuyaExampleCommon.Player));
  24.         float joy1axis2 =
  25.             Input.GetAxisRaw(string.Format("Joy{0} Axis 2", (int)OuyaExampleCommon.Player));
  26.         transform.position = new Vector3(joy1axis1, 0.5f, -joy1axis2);
  27.         lblAxis1.text = joy1axis1.ToString("F5");
  28.         lblAxis2.text = joy1axis2.ToString("F5");
  29.  
  30.         if (Mathf.Abs(joy1axis1) >= 1.0f ||
  31.             Mathf.Abs(joy1axis2) >= 1.0f ||
  32.             Input.GetKey(KeyCode.RightArrow))
  33.         {
  34.             m_timerSound = DateTime.Now + TimeSpan.FromMilliseconds(SOUND_TIME);
  35.         }
  36.  
  37.             if (m_timerSound < DateTime.Now)
  38.             {
  39.                 audio.volume = 0;
  40.             }
  41.             else
  42.             {
  43.                 float elapsed = (float)(m_timerSound - DateTime.Now).TotalMilliseconds;
  44.             audio.volume = (DateTime.Now < m_timerSound) ? 1 : Mathf.Lerp(audio.volume, 0, 1f - elapsed / SOUND_TIME);
  45.             }
  46.         }
  47. }
clone this paste RAW Paste Data