How to Play random animation for Attack

I am trying to play a random attack animation each time I call a function. I have 3 animations DAT will be trigger using 3 trigger parameters. After clicking the button many times, it tends to keep the triggers on even after I have stopped pressing. What I want is DAT each time I press the button n random animation is played and the next click plays a new one. I am new to unity so if you have better ways of doing this, am open for suggestions.

I was going to suggest the below which is effectively the same as your code with fewer lines.

void Attack()
    int randomNumber = Random.Range(1, 4);
    anim.SetTrigger("atk" + randomNumber);

It sounds like you might be running into the same issue as posted here. You should try some of the suggestions posted on that thread.

Hope that helps!

Unfortunately there seems to be no generic way of doing this. I googled and searched the docs but simply could not find a way. Random transition is definitely a feature that should be supported out of the box but I could not find a way to get number of outgoing transitions from a state in runtime either — so much for making a generic randomizer.

That said… below is code for SMBRandomInt.cs which I just made. It makes setting up a random transition from the Entry node of sub state machine as easy as I could make it for now.

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

// Randomizes an integer parameter at transition to the Entry node of a state machine. This parameter can be used for choosing a random transition out from the Entry node. 
// How to setup a random transition from the Entry node of a sub state machine: 
//  1) Attach SMB Random Int to Your Sub State Machine 
//     - copy this SMBRandomInt.cs to your Assets folder 
//     - click your sub state machine in Animator, select "Add Behavour" in object inspector and choose SMB Random Int
//  2) Configure SMB Random Int in Inspector
//     - Enter new name for the random parameter if you are not happy with RandomInt
//     - Add the chosen parameter to your Animator's list of parameters as Integer type
//     - Configure MaxValue = [number of outgoing transitions - 1]
//  3) Setup outgoing transitions
//     - default transition always runs if any other doesn't -- let's have it run if our random value is 0
//     - for the first additional outgoing transition set condition [random parameter name] Equals 1
//     - for the second additional outgoing transition set condition [random parameter name] Equals 2
//     - ... etc etc ... 

public class SBMRandomInt : StateMachineBehaviour
    [Tooltip("The parameter name that will be set to random integer value. You must add this into your animator's parameter list as an Integer.")]
    public string parameterName = "RandomInt";

    [Tooltip("Minimum generated random value.")]
    public int minValue = 0; 

    [Tooltip("Maximum generated random value.")]
    public int maxValue = 1; 

    // OnStateMachineEnter is called when entering a state machine via its Entry Node
    override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash) {
        int value = Random.Range(minValue,maxValue+1);


Alternatively you can use the animator sub-state machine to play a randomized animation.
I’ve seen a youtube video explaining this process but will link to the official video.

Hi @darchyneer,

Try the code that I used:

int randomAttack = Random.Range(1, 4);
playerAttackAnim.SetInteger(“randomAttack”, randomAttack);

You need 2 Parameters in your Animator:

  1. randomAttack - Int
  2. Attack - Trigger

Set both parameters in your transition, set “randomAttack” to equals “1” for your attack1, and so fort.

Hope this helps.

hello everyone, I’ve brainstormed this problem and here’s my solution:


  • No need to use Animator.parameter
  • list of nameHashes is updated automaticly at OnBeforeSerialize
  • use Crossfade(stateNameHash) to change animation
    Long story short:

Animator.StateMachine with child random animations and StateMachineBehaviour on it, which contains editor-cached nameHashes for each random state and execute Animator.Crossfade(randomHash) at OnStateEnter. There is also some minor checks to not execute Crossfade infinitely.