A lot happening now, I finally got the problem with playing random animations sorted and now my moon animations are playing nice and smoothly but holy fuck it was annoying working it all out. In fact it was so complicated that I wrote up a very long post about it on the unity forums where I was asking for help but eventually started finding answers.
Be warned! Very long and technical post, skip down after the quoted text to get back to my normal posting!
Okay, about ready to post an explanation now, so there were several things that went on with my investigating when it came to just getting random animations working. The first was me checking out legacy animations because that looked the most promising with the code, I decided that I'd try out using an array to play my animations, this did work however low and behold because I was using sprite based animations they wouldn't even play correctly.
Back to square one, this is why I made this thread, I simply hadn't realised that it wasn't possibly to use legacy animations for sprite based work. I then eventually after all this posting went back to this method I had looked up awhile ago but simply hadn't understood because I didn't know enough about transitions generally and the way states worked.
Now though I have absolutely everything setup perfectly and since it was so damn difficult I'm doing this write up to help newbies like me with it. First up check this guys' tutorial even though he misses out a lot of stuff because he was only doing it one way it's very helpful to watch through it all, it does go on a bit but it will help you with the basics of the technique.
To save time for everybody, here's the code that I ended up with, now, one very important thing to note is that for this to work sensibly you have to have the same amount of integers as animations you want to play randomly. Otherwise what will happen is the code will flip through integers that aren't there making the animations look strange.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoonPlayRandomAnimation : StateMachineBehaviour {
public string m_ParameterName = "MoonID";
public int[] m_StateIDArray = { 0, 1, 2 };
// OnStateMachineEnter is called when entering a statemachine via its Entry Node
override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
{
if ( m_StateIDArray.Length <= 0 )
{
animator.SetInteger ( m_ParameterName, 0 );
}
else
{
int index = Random.Range ( 0, m_StateIDArray.Length );
animator.SetInteger ( m_ParameterName, m_StateIDArray [index] );
}
}
}
He explains this in the video, but what this code does is it checks for transitions named as the parameter assigned and then randomly cycles through them using random.range the integers are assigned to these transitions and therefore the animations and that's how this code works. Go through this first so you know what I'm on about and get everything set up.
What he doesn't explain is that there are various tweaks you need to do to make sure the timing of the animations is correct and you may have your own way of doing things. Now the most obvious problem I immediately ran into was that animations were overlapping each other and playing instantly, all very weird, there were three things I had to do to solve this. One was to simply change the transition duration and exit time in order for it to have an effect on your animations you need to go into the sub state machine you've setup for the code and click on the white this made the animations last longer so they wouldn't skip about.
This still wasn't enough though, as I was still having issues with timing and my default animations were playing over each other rather than playing randomly and it made everything look weird. What I did was I made the default orange state machine blank, I removed the animation entirely and that actually even helped with the timing because there was a gap between the animations when they changed it would then switch to the sub state machine smoothly and pick an animation at random.
Again though, another problem, sometimes though the animation wouldn't end correctly and just loop itself, this one proved easiest to fix I simply went into the animation clips one by one and disable loop time. This meant the animation actually ended now and even had a nice little delay due to the blank state machine I had made as the default state machine rather than two of the same animation.
That's just about the end of my writeup, I really hope this helps newbies who were struggling with what should be something simple to do, if anyone has any corrections and so on to make feel free but I think I got everything. These few days have been absolute hell dealing with this problem but I'm glad I got it sorted.
Why couldn't Unity just code in some kind of simple randomiser state? I had an extra think about this and if you wanted to use the code here for some kind of randomised attack animations perhaps then you should be able to use a public animator object and 'SetInteger' to cycle between the animations whenever you do a mouse click. Or I guess you could even just grab the integers and set them directly through the script.
After all that I've finally found out how to randomly play my animations and learned more than I like to about Unity's animation systems. This means now there is only the ending scene to finish which I have mostly already got prepared and then I'll have to do a full playthrough of the game to make sure everything's working.
I will let you guys know in a fresh post when the game is about to release, it will be available on my website immediately but I'll need to submit it to steam and go through that process as I mentioned in my previous posts. I don't even know if it will be accepted, hopefully it will but I'll just have to see what happens now.
There's minor stuff to do like the credits but that will be easily done I think I just need to make a scene with some credentials on it that won't be difficult, time to finish this thing!
what language do you use to write the code
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
C# also known as C-Sharp.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit