Forum rules - please read before posting.

Feature request: Random animation selection

edited September 2018 in Engine development
It would be awesome if instead of just having just an Idle_D animation we could add any number of variations of that sprite (Idle_D1, Idle_D2 etc). And whenever the Idle_D animation should be activated, They would be played in sequence in a random order.

This would make it really simple to do variations like making the player shrug his shoulder some times, blink or whatever. Also useful to introduce some variations while speaking.

Comments

  • Would it not be easier to have a really long idle animation with the character doing various things from time to time?
  • Doing that now but that takes the randomness out of it.
  • For anything more involved than a single set of standard idle/walk/talk animations, you should rely on Sprites Unity Complex animation mode, which relies on Animator parameters for your character's animation playback.

    What you can then do is plug additional idle animations into your Animator Controller, and have them tie into a "RandomIdle" integer parameter that affects which animation is transitioned to.

    This "Random Idle" integer parameter can then be set randomly at fixed intervals in a simple script attached to the character, i.e.:


  • That's very interesting, will definitely check that out..
  • This is hard :P

    The Brain2D_SpritesUnityComplex character provided in the 2D demo is much appreciated but I still can't figure out how to get the random Idle states to work..

    The script you linked to should be placed on the character game object right? And then how should I set up the transitions?

    I don't suppose anyone else has done this and could show me a screen dump of the animation controller or even a working sample?

    Thanks!
  • The script can be placed anywhere, since all the variables it makes use of can be set in the Inspector.  It's best placed on the character's root, however.

    Ignoring the actual transitions for the moment, have you gotten the parameter set up and changing randomly?  On your Animator, create an integer parameter (named "RandomIdle" unless you change it in the script's Inspector), and assign the Animator in the Inspector.  You should find that it changes value every few seconds when the game is running.

    Then, just deal with having random animations only place for a fixed direction, i.e. down.  Add however many alternative idle animations you have for your character when facing down to the Animator, and have your "normal" downward-idle animation transition to them after a set time - each using a different value of RandomIdle.

    Only when you're happy with that should you look to incorporate the other directions.
  • Ok, with your help I think I get the idea behind this now. It is pretty convoluted however and I think I'm going to give up on the randomness for now..

    I would still love to see a simpler implementation for this. I did mess around with Unity's support for triggering method called from animation key frames which provided a somewhat cleaner solution, but still far from ideal for something that is quite common.

    Another solution would be to fire an event whenever the player has been idling for a while (perhaps the frequency and randomness could be set in the settings manager).

    Thanks four your help in guiding me through this though!
  • edited October 2018
    I would argue that relying on parameters is more in-line with the Unity "best practice" way of working, but if you really want to stick to Sprites Unity mode and just change the name of the "Idle" animation randomly - you can do that with a custom script:


    That'll modify your original Idle name to e.g. Idle1_D, Idle2_D etc at random intervals.
  • You're probably right and admittedly I still have a lot to learn about Unity. But thanks for this script anyway, I might just go down the path of least resistance on this one.
  • I'd like to use that script you posted, but it should only start randomizing once the original Idle animation is played. Would it be possible to add custom events for when an animation is started/stopped?
  • That kind of precision is exactly why I recommended the parameter-based approach.  Having Unity check for animation playback states can be very imprecise and not a good way of working.
  • Even if it's just triggered by events that fires quite rarely as compared to checking in a tight loop?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.