Hi everyone, I’m a unity beginner and this is my first post here.
I’m working on a character that needs to driven by a person in a live presentation with a basic realtime microphone talk (I’m using Salsa).
The character have some idle states that are played in a random order and for a random amount of time,
and at any time the “player” can take over and run states related to his mood, or what he’s talking bout.
Those activated states can be facial expressions or a full body animation.
So I started to mock up that state machine, here “left” and “right” are still idle poses, it’s just some slow bending so the character is not too static:
Each transition have an integer id and in the state machine behavior a timer is ran and switch to a new
state id when the duration is over.
public class idle_blending : StateMachineBehaviour {
private float timer = 0;
private float rnd = Random.Range(5f, 10f);
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
if (timer < rnd) {
timer += Time.deltaTime;
} else {
animator.SetInteger("idleIndex", Random.Range(0, 3));
rnd = Random.Range(5f, 10f);
timer = 0;
return;
}
}
It’s kind of working, but the problem is I don’t know how many out transitions each state is connected to and I’m currently setting an arbitrary maximum, which means I can have a miss when the random number is higher that the transitions count.
Now my questions are:
-Am I doing it the right way?
-I’m not sure if I need to do that in a MonoBehavior or a stateMachineBehavior? It seems it can be done in both.
-How do I access the number of output transitions of the current playing state so I can get a random integer between 0 and the state transitions count -1 ?
And I need to do this at runtime of course.
Any help will be much appreciated!
Thanks.