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