Home Unity

Question Random Parameter

Greg DAlessandro
polycounter lvl 11
Offline / Send Message
Greg DAlessandro polycounter lvl 11
Anyone know how to make it where in Animator you can set up a transition to randomly choose between 2 states?

Ie: State A - > to either State B or State C. Thank you.

Replies

  • Hyshinara
    You can write a script that runs when StateA is activated and creates a randomised boolean (true / false), which you can use as a parameter in the Animator.

    I'm writing this in UnityScript (.js), but it can easily be translated to C# (.cs) if needed:
    1. #pragma strict
    2.  
    3. var useStateB : boolean;
    4.  
    5. function Start () {
    6. useStateB = Random.value > 0.5f; //this returns 50-50 true or false
    7. }

    Then simply make a connection from StateA to StateB with "useStateB = true" and one from StateA to StateC with "useStateB = false".

    Of course, using a boolean only works with two states. I you have more than two, you can use a randomised integer instead:
    1. #pragma strict
    2.  
    3. var useState : int;
    4. var amountOfStates : int = 3; //let's use 3 states for this example
    5.  
    6. function Start () {
    7. useState = Random.Range(1, amountOfStates + 1); //returns 1, 2 or 3: Random.Range includes the min value, but excludes the max value => add 1 to the desired max value
    8. }
Sign In or Register to comment.