Slate Forums › Support › Randomization in Animation › Reply To: Randomization in Animation
I am using bones rotations and positions in the animation clip. I guess I am after specifics for example how to connect custom mixer to animation graph. I know how to do it in a simple script but I don’t see an option to integrate it with your API. Putting separate track above random blinking track causes my character to blink with closed eyes (no matter how weird it sounds). This is my blinking clip. The best scenario for me is to cover whole track with a random blinking and put specific blinks in between, but then I can’t guarantee that I won’t start specific blink with eyes closed, so I need a way to blend it nicely no matter in what state are eyes because of the random blinking. I thought about tweaking whole track weight, but maby you know a better solution.
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
[Name("Random Blinking Clip"), Description("Makes character blink randomly.")] public class RandomBlinkingClip : PlayAnimatorClip { [Header("Parameters")] [SerializeField] private float _blinkSpeed = 10f; [SerializeField] private Vector2 _blinkIntervals; private BlinkState _blinkState; private float _timeToBlink; private float _blinkProgress; protected override void OnUpdate(float time, float previousTime) { switch (_blinkState) { case BlinkState.PickInterval: PickInterval(); break; case BlinkState.WaitForBlink: OnIdle(); break; case BlinkState.EyesClosing: OnEyesClosing(); break; case BlinkState.EyesOpening: OnEyesOpening(); break; } } private void PickInterval() { _timeToBlink = Random.Range(_blinkIntervals.x, _blinkIntervals.y); _blinkState = BlinkState.WaitForBlink; } private void OnIdle() { _timeToBlink -= UnityEngine.Time.deltaTime; _blinkState = _timeToBlink > 0 ? BlinkState.WaitForBlink : BlinkState.EyesClosing; UpdateClipOnTrack(0f, 1f); } private void UpdateClipOnTrack(float clipTime, float weight) { track.UpdateClip(this, clipTime, 0f, weight); } private void OnEyesClosing() { _blinkProgress += UnityEngine.Time.deltaTime * _blinkSpeed; float weight = Math.Min(_blinkProgress, 1f); float clipTime = weight * animationClip.length; UpdateClipOnTrack(clipTime, weight); _blinkState = _blinkProgress < 1f ? BlinkState.EyesClosing : BlinkState.EyesOpening; } private void OnEyesOpening() { _blinkProgress -= UnityEngine.Time.deltaTime * _blinkSpeed; float weight = Math.Max(_blinkProgress, 0f); float clipTime = weight * animationClip.length; UpdateClipOnTrack(clipTime, weight); _blinkState = _blinkProgress > 0f ? BlinkState.EyesOpening : BlinkState.PickInterval; } } |