Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by darchyneer · May 08, 2017 at 07:56 AM · scripting problemrandomanimationsparametersanimation controller

How to Play random animation for Attack

alt text

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.

code.jpg (72.8 kB)
animator-and-parameters.jpg (45.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
4

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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);
    }
 
 }
 

  
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ziplock9000 · Jun 26, 2019 at 01:43 PM 0
Share

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.

avatar image
0

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.

https://unity3d.com/learn/tutorials/topics/animation/animator-sub-state-machine-hierarchies?playlist=17099

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ziplock9000 · Apr 05, 2019 at 05:08 PM 0
Share

None of what you've said explains how to do the randomness, which is the question.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges