You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB
C#

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);
}
}
}
}