Search Unity

Random Idle with Blend Tree

Discussion in 'Animation' started by chaosmonger, Nov 14, 2019.

  1. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    Following some threads around, I'm trying to get a random Idle animation with blend tree and some custom script, but it's not working...

    Basically I've a standard Idle animation and I'd like that once in a while, randomly, a new Idle animation is played (called Idle_Random).
    I've set a Blend Tree with the two idles and a float parameter "IdleRandom". If this float parameter is equal to 0, the character is in standard Idle, if the float is equal to 1, the character plays the occasional Idle anim.
    I've set an event at the end of the standard Idle animation, that calls the "RandomIdle". But unfortunately is not working. When the random range is triggered, my character freezes and is not playing anything.
    Here the script attached to my character.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class RandomizeIdleType : MonoBehaviour
    5. {
    6.     Animator animator;
    7.     void Start()
    8.     {
    9.         animator = GetComponent<Animator>();
    10.     }
    11.     void RandomIdle()
    12.     {
    13.         if (Random.Range(1, 5) == 1)
    14.         {
    15.             animator.SetFloat("IdleRandom", 1);
    16.         }
    17.     }
    18. }
    And attached a screenshot of the Blend Tree parameters.
    upload_2019-11-14_13-52-27.png

    The method called from the last frame of my standard Idle anim.
    upload_2019-11-14_13-52-55.png

    And my animator screen with all the branches.
    upload_2019-11-14_13-52-4.png


    Why is not working?
    It's because of the nested Blend Trees?
    Something wrong with the script?

    Please let me know!
    Thanks!
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    It's always hard to help with issues like this due to the way the Animator Controller system scatters your data and logic across so many different areas. Maybe the key is in one of those screenshots if you look close enough, or maybe not, it's hard to trace cause and effect through a setup like that.

    The 3D Game Kit has a working implementation of random idles and I made an example which tries to explain how that character's various parts were implemented in order to compare with how the same could be done using Animancer, including the random idles.
     
  3. chaosmonger

    chaosmonger

    Joined:
    Jan 23, 2019
    Posts:
    71
    UPDATE: I've found the problem. Since my Idle_Random wasn't set to "loop", it was somehow played and stopped in the background, therefore when called it was frozen.
     
    MysleyMakers likes this.