
Sound Timer
By: a guest on
Jun 11th, 2013 | syntax:
C# | size: 1.44 KB | views:
49 | expires: Never
using System;
using UnityEngine;
public class ouyatest : MonoBehaviour
{
public UILabel lblAxis1;
public UILabel lblAxis2;
public AudioSource BeepSource = null;
private DateTime m_timerSound = DateTime.MinValue;
private const float SOUND_TIME = 200;
// Use this for initialization
void Start()
{
Input.ResetInputAxes();
OuyaExampleCommon.Player = OuyaSDK.OuyaPlayer.player1;
}
// Update is called once per frame
void Update()
{
float joy1axis1 =
Input.GetAxisRaw(string.Format("Joy{0} Axis 1", (int)OuyaExampleCommon.Player));
float joy1axis2 =
Input.GetAxisRaw(string.Format("Joy{0} Axis 2", (int)OuyaExampleCommon.Player));
transform.position = new Vector3(joy1axis1, 0.5f, -joy1axis2);
lblAxis1.text = joy1axis1.ToString("F5");
lblAxis2.text = joy1axis2.ToString("F5");
if (Mathf.Abs(joy1axis1) >= 1.0f ||
Mathf.Abs(joy1axis2) >= 1.0f ||
Input.GetKey(KeyCode.RightArrow))
{
m_timerSound = DateTime.Now + TimeSpan.FromMilliseconds(SOUND_TIME);
}
if (m_timerSound < DateTime.Now)
{
audio.volume = 0;
}
else
{
float elapsed = (float)(m_timerSound - DateTime.Now).TotalMilliseconds;
audio.volume = (DateTime.Now < m_timerSound) ? 1 : Mathf.Lerp(audio.volume, 0, 1f - elapsed / SOUND_TIME);
}
}
}