Advertisement
AnthonyC

HotspotAudioCueV2 / Anthony / NextRealityNews HowTos

Apr 17th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class HotspotAudioCue : MonoBehaviour {
  4.  
  5.     public AudioClip[] clips;
  6.     public Transform objectToOrbit;
  7.  
  8.     private AudioSource audioSource;
  9.     private float revolutionTime;
  10.     private Vector3 rotationDirection;
  11.     private bool isActive;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.         audioSource = GetComponent<AudioSource>();
  16.     }
  17.  
  18.     private void OnEnable()
  19.     {
  20.         Hotspot.OnEntered += OnHotspotEnter;
  21.     }
  22.  
  23.     private void OnDisable()
  24.     {
  25.         Hotspot.OnEntered -= OnHotspotEnter;
  26.     }
  27.  
  28.     private void OnHotspotEnter()
  29.     {
  30.         isActive = !isActive;
  31.  
  32.         if (isActive)
  33.         {
  34.             int randomNum = Random.Range(0, clips.Length);
  35.             audioSource.clip = clips[randomNum];
  36.  
  37.             revolutionTime = 360 / audioSource.clip.length;
  38.             rotationDirection = new Vector3(Random.Range(0, 2), 1, Random.Range(0, 2));
  39.  
  40.             audioSource.Play();
  41.         }
  42.         else
  43.         {
  44.             audioSource.Stop();
  45.         }
  46.     }
  47.  
  48.     void Update()
  49.     {
  50.         if (isActive)
  51.         {
  52.             transform.RotateAround(objectToOrbit.transform.position,
  53.             rotationDirection, revolutionTime * Time.deltaTime);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement