ProceduralHats.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. namespace Valve.VR.InteractionSystem.Sample
  5. {
  6. public class ProceduralHats : MonoBehaviour
  7. {
  8. public GameObject[] hats;
  9. public float hatSwitchTime;
  10. private void Start()
  11. {
  12. SwitchToHat(0);
  13. }
  14. private void OnEnable()
  15. {
  16. StartCoroutine(HatSwitcher());
  17. }
  18. private IEnumerator HatSwitcher()
  19. {
  20. while (true)
  21. {
  22. yield return new WaitForSeconds(hatSwitchTime);
  23. //delay before trying to switch
  24. Transform cam = Camera.main.transform;
  25. while (Vector3.Angle(cam.forward, transform.position - cam.position) < 90)
  26. {
  27. //wait for player to look away
  28. yield return new WaitForSeconds(0.1f);
  29. }
  30. ChooseHat();
  31. }
  32. }
  33. private void ChooseHat()
  34. {
  35. SwitchToHat(Random.Range(0, hats.Length));
  36. }
  37. private void SwitchToHat(int hat)
  38. {
  39. for (int hatIndex = 0; hatIndex < hats.Length; hatIndex++)
  40. {
  41. hats[hatIndex].SetActive(hat == hatIndex);
  42. }
  43. }
  44. }
  45. }