- Home /
How to trigger state transition from C#
I have a list of transitions out of a state, and I would like to select one at random, but I don't know how to activate it from a statemachine behaviour. Given the potential number of child states, I would prefer not to define a transition parameter for each. Has anyone tried this?
I could not fully understand it.
Are you talking about the Animator, or you have a custom scripted state machine that handles the state of something?
Showing some code could help.
Imagine that you have a state with two transitions leading out of it. I want to select which transition to use randomly, rather than being based on a condition. Is that any clearer? If not, let me know and I'll try again.
The answer is really dependent on the code of your state machine, without it we can't do much
Answer by ZeBarba · Sep 27, 2019 at 03:01 PM
With the details provided, I would do something like that:
Set a data structure with all the possible transitions out of the state. Array, list, what suits your project better. If they are a method, that's even better.
Make a random number generator method somewhere in your project that the parameter is the Length of the data structure in bullet point 1 and return an 1 on the range 0 - Length. (could be an extension method also)
Than call the data structure using the random number generator as the index, like:
possibleTransitions[randonNumberGen];
The problem is that there are several ways to script a "state machine", from a simple switch to classes for each state, etc. You will have to wing it and adapt it to your project. This is heavily dependent of your code.
Also, the quality of randomness will be a factor of how you do your random number generator.
Your answer
Follow this Question
Related Questions
is it possible to read states / transitions from mecanim in runtime ? 1 Answer
how to make multiple transition from one state? 1 Answer
How can I make a state pass after a reversed state? 0 Answers
How to implement the "State" pattern in Unity using an abstract class? 1 Answer
Interface state structure with different type of enemies 0 Answers