Random Range for death animations not working well?

Hi,

I’m using Random.Range to play one of 8 death animations randomly when the bool that checks if the player has died becomes true. It works, but it seems like it’s always the first 4 animations that are played and rarely, it even doesn’t play any of the death animations. Here’s my code:

  void Start()
    {
        rand = Random.Range(1, 8);
        _animator = GetComponent<Animator>();
        dead = false;
    }

void Update()
    {


        if (h.dead1True)

        {
            if (photonView.isMine)
            {
                StartCoroutine(WaitdeathPlane());
            }


            if (rand == 1)
            {
                hipshakeDeath = true;
                //xploDeath = false;
            }

            if (rand == 2)
            {
                //dead = false;
                xploDeath = true;
            }
            if (rand == 3)
            {
                headSlapDeath = true;
                //xploDeath = false;
            }

            if (rand == 4)
            {
                //dead = false;
                crouchDeath = true;
            }
            if (rand == 5)
            {
                saluteDeath = true;
                //xploDeath = false;
            }

            if (rand == 6)
            {
                //dead = false;
                dead = true;
            }
            if (rand == 7)
            {
                sprald = true;
                //xploDeath = false;
            }
            if (rand == 8)
            {
                dance = true;
                //xploDeath = false;
            }

Is there a better way to make this work every time and to be sure that the random range choose between all 8 animations instead of just the same four that it seems it’s choosing?

Check docs on Random.Range(int, int) - you will never get 8 in this code. Also integer random equal distribution is not guaranteed. You’d better use float. And you don’t need all those variables. Just use animator.SetInt or SetFloat and edit the transitions conditions to use numbers, there are comparsions less or equal, more or equal available.

1 Like

If you are worried about seeing some pattern in the random selection of animations, you could just simply keep a list or just store the previous death and then prevent choosing the same animation again. Randomize number again if you get the same as the previous one, or just increment/decrement the value.

1 Like