Randomize animation in Unity

You have multiple animation for a single state and want to play the animations randomly? Want to start an animation from a random frame? Want random animation speed? You are in the right place. In this Tutorial we are going to see how to randomize all aspects of animation.

I am Using Unity 2021.1.13 along with HQ Fighting Animation FREE asset from Unity asset store for this tutorial. Let’s get started.

Playing random animation

Start a new project and import the asset into Unity. There is an demo scene included with the asset. Open the demo scene and remove the script used to control the animation state. We are going to use our own script to randomize the animation state.

Open animator by going to Window>Animation>Animator. Delete other states and keep only the attach states. There are 4 attack states in our controller that we will play in random. Remove all the parameters and create a new integer variable called ‘Attack_no’.

Set up the transition from Idle to each state based on different Attack_no as in the image below.

Unity animator window
animation random condition

Set Attack_no from 1 to 4 for each attack. Now let’s add a script to randomize the integer and play different animation at random.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Anim_random : MonoBehaviour
{
    Animator anim;
    bool generated=false;
    // Start is called before the first frame update
    void Start()
    {

      anim=GetComponent<Animator>();  
    }

    // Update is called once per frame
    void Update()
    {
        if(anim.GetCurrentAnimatorStateInfo(0).IsName("Idle") && !generated)
        {
            anim.SetInteger("Attack_no",Random.Range(1,5));
            generated=true;

        }
        
    }
    public void animation_event()
    {
        generated=false;
    }
}

The transition between animation states takes more than one frame. So multiple random number gets generated. To avoid this use a bool and set it as true when a random number is generated. You can set it to false once the animation clip is played.

The above script uses an Animation event form this purpose.

Adding an Animation event

To add an animation event follow the steps below.

  1. Create a public function with the conditions you want to execute. In our case we want to reset the Boolean to false.
  2. Select the animation clip from the project window.
  3. Find the Events tab and click on add event(its a symbol on the left top).
  4. Drag and drop the script to the object reference.
  5. Enter the name of the function without “()” in the Function Variable.
animator event

Start Animation at a Random frame

To play an animation clip from a random frame you can pass the optional third parameter normalized time of Animator.Play() function. Normalized parameter taken a float in the range 0 to 1. Depending on the value to the normalized time the animation stars playing. You can randomize this value to start animation at random frame.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Anim_random : MonoBehaviour
{
    Animator anim;

    // Start is called before the first frame update
    void Start()
    {

      anim=GetComponent<Animator>();  
      anim.Play("Jab",-1,Random.Range(0.0f,1.0f));
    }

    
}

Randomizing Animation Speed in Unity

Animation speed can changed from the script using the Animator.Speed parameter. It takes a float value. Make sure not to make the top value too big unless you have a very slow animation clip. Here is a sample script you can use.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Anim_random : MonoBehaviour
{
    Animator anim;

    // Start is called before the first frame update
    void Start()
    {

      anim=GetComponent<Animator>();  
      anim.speed = Random.Range(0.0f,4.0f));
    }


    
}

Hope you got some useful information from the tutorial. If you have any other question regarding randomizing an animation, feel free to leave it in the comment section below. You can read our post on generating a random number in Unity for more details on random number generation.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.