-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomLightBlinker.cs
51 lines (48 loc) · 1.51 KB
/
RandomLightBlinker.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomLightBlinker : MonoBehaviour
{
Animator animator;
bool animated => animator != null;
const float MIN_IDLE_TIME_SECONDS = .5f*60f;
const float MAX_IDLE_TIME_SECONDS = 3f*60f;
public bool _blinks = false;
public float _idle_time = 0f;
float idleClipLength = 0f;
private void Awake()
{
animator = GetComponent<Animator>();
var clips = animator.runtimeAnimatorController.animationClips;
/*
foreach (var clip in clips)
{
if (clip.name == "Idle")
idleClipLength = clip.length; break;
}*/
idleClipLength = animator.GetCurrentAnimatorStateInfo(1).length / animator.GetCurrentAnimatorStateInfo(1).speed;
Debug.Log($"Idle clip length is {idleClipLength}");
}
void Start()
{
StartCoroutine(Waiter());
}
IEnumerator Waiter()
{
while (animated)
yield return Randomiser();
}
IEnumerator Randomiser()
{
float random = Random.Range(MIN_IDLE_TIME_SECONDS, MAX_IDLE_TIME_SECONDS);
_idle_time = random;
//animator.SetBool(name = "isBlinking", true);
_blinks = true;
Debug.Log("Blink!!!");
//yield return new WaitForSeconds(idleClipLength);
animator.Play("LightBlinkGentle");
//animator.SetBool(name = "isBlinking", false);
_blinks = false;
yield return new WaitForSeconds(random);
}
}