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.
Answer by BackslashOllie · May 08, 2017 at 11:35 AM
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!
Answer by Marhaikal · Jul 11, 2017 at 03:54 AM
Hi @darchyneer,
Try the code that I used:
int randomAttack = Random.Range(1, 4); playerAttackAnim.SetTrigger("Attack"); 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.
Answer by Tomi-Tukiainen · Jun 21, 2019 at 06:29 PM
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);
animator.SetInteger(parameterName,value);
}
}
I ended up using a system that used a weighting system, so that say a certain idle animation is more likely to play than another. This meant I stored an array of weights rather than just a max/$$anonymous$$. The rest is the same.
Answer by theANMATOR2b · May 08, 2017 at 01:00 PM
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.
None of what you've said explains how to do the randomness, which is the question.
Answer by mitaywalle · Jun 02 at 12:24 AM
hello everyone, I've brainstormed this problem and here's my solution: https://github.com/mitay-walle/Unity3d-AnimatorRandomBehaviour
Pros: - No need to use Animator.parameter - list of nameHashes is updated automaticly at OnBeforeSerialize Cons: - 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.
Your answer
Follow this Question
Related Questions
Why my animator is freezzed after WaitForSeconds(Delay) 1 Answer
Access Animation From Another Script 1 Answer
Animations and Animator don't work - 2D Platformer Game for Android 0 Answers
Different random number - Same script - Different cloned object 0 Answers
How to rotate spine by the script, while an animation from the animator is playing? 1 Answer