Home Unity

Question Random Parameter

Greg DAlessandro
polycounter lvl 6
Offline / Send Message
Greg DAlessandro polycounter lvl 6
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:
    #pragma strict
    
    var useStateB : boolean;
    
    function Start () {
    	useStateB = Random.value > 0.5f; //this returns 50-50 true or false
    }
    

    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:
    #pragma strict
    
    var useState : int;
    var amountOfStates : int = 3; //let's use 3 states for this example
    
    function Start () {
    	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
    }
    
Sign In or Register to comment.