Search Unity

playing nicely with mecanim and my own state controller?

Discussion in 'Editor & General Support' started by TheSin, Nov 24, 2012.

  1. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Hi guys, mecanim look great and offers so much for animation but ive spent alot of time writing my own 360 character/rigidbody controller and state system. In it have OnEnter/OnUpdate/OnExit plus many more custom calls in my own state controller. It's allowing me great control over my characters with layers and states and states changes etc and ive been working on a 3d platformer engine which implements it.

    I already have some examples running a full 360 platformer ala mario galaxy and my character has potentionally unlimited states ie spawns, idles, emotes, jumps, double jumps, flips, strafes, quickturns, climbs, dives, walk, jogs, health layers, lands, falls, edge grabs etc etc etc. I also have my own system in place to switch states etc so i can go from a fall to a land with a simple line of code which can check for height fallen, surface landed on etc and go to a different land.

    My system currently fires and clears animations, blends/sets weights in the OnEnter/OnUpdate/OnExit calls for a state using the animation system currently in unity. all i really need from mecanim is the exact functionality plus target weights/ik/avatar stuff. Is that possible via scripting ie load clips and amnually set weights, blends etc or read the data from current blend trees? also my system uses a rigidbody+collider+controller script(allowing me to rescale, rotate and uses physic objects ie bridges etc), is it possible for mecanim to read/interchange data, variables etc?

    Here is a link to some videos of my system

    http://www.youtube.com/user/sindiinbonnienclyde?feature=mhee
     
    Last edited: Nov 24, 2012
  2. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    We dont have the OnEnter/OnUpdate/OnExit callbacks in Mecanim, but you can easily implement your own system over it!

    Look at :

    animator.GetCurrentAnimatorStateInfo(layerIndex);
    animator.GetNextAnimatorStateInfo(layerIndex)
    animator.IsInTransition(layerIndex);
    animator.GetAnimatorTransitionInfo(layerIndex);

    Basicaully this offers a way of mapping your AI states to the Animation states. I also suggest using State tags in Mecanim. I've named those to my AI states so that the mapping would be easier to do !

    Hope this helps!

    super nice project btw!
     
  3. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Thankyou for the reply. Hopefully this helps me as my own stateController system is pretty complex and was hoping to integrate mecanim before releasing it. My system overrides unitys awake/start/fixedupdate/update/lateupdate plus all collision/trigger calls to allow for a very powerful system. I have my own custom controller that works in full 360 and is super simple to use.

    If i can integrate mecanim for anim( although i found no issues with legacy anims) i think the community will love what ive created!!

    Mario galaxy style games will be easy to make plus almost any other character mechanics...hopefully!

    I already implement a layering system too where arms avoided objects, legs lifted when standin on posts etc and look forward to the whole mecanim ik and targeting system. Thanx guys!
     
  4. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    cant wait to see your game played with a Teddy Bear (using Mecanim retargeting!)

    pp
     
  5. iiley

    iiley

    Joined:
    Sep 4, 2012
    Posts:
    14
    I have encountered a similar situation, your discussing help a lot.

    Here are few other little question:
    ----1. Is there a way to know the time length of a transition?(how long a transition will take to complete). I saw AnimatorTransitionInfo does not have a length similar property.
    ----2. Is there a way to do real instant transition, switch animation immediately(at least in next frame). (Cygon4's way will take 2 frames, see http://answers.unity3d.com/questions/355576/how-to-make-a-instant-transition-with-mecanim.html)
     
  6. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Haha sounds like an idea. Already have an actual character idea and demo idea im doing side by side with this project. Hoping it wont be a major headache intergrating mecanim. I regularly put new videos up and just put up a collision one ala mario 128.

    Yet to test mecanim with it but excited with what i can do with it...swinging on ropes, better ledge climbing animations, better leg ik when climbing monkey bars etc!!
     
  7. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Sorry for another question but an answer would be great. Can i just manually adjust the mecanim animation clips and not even touch the animator, blend trees etc? I really have no use for the state machine in mecanim as my own statecontroller does it all for me.
    Ps i havent got around to downloading unoty3d 4.0 as ive no internet for now other than my phone and limited data.
     
  8. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    In the future, we want to offer the ability to be able to completely control the animator without "enforcing" StateMachine logic. Unfortunately we could not make it for 4.0. For now , we offer the ability to force a State at a certain time, so theoritically, using AnyState transitions + ForceStateNormalizedTime, you could have similar behaviour ( but with quite some more work)
     
  9. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Thankyou again for the reply. That sounds good and i bit the bullet and download unoty3d 4.o and after a quick mecanim play im super impressed. Just an initial issue, can i user an 'or' for variables as it appears as if you can set vars for conditions but it does an 'and' check for conditions to change states when multiple vars are used.

    Im super impressed though, managed to blend idle, sneak, walk, jog, dash based on analogue range within 5mins of use to replicate my own system.

    Ps im guessing i can set multiple transitions for one state to another each woth a var. This would give the same 'or' check.
     
  10. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Exactly, you just set multiple transition to emulate the 'or'
     
  11. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150

    Thanks. Have played around with mecanim for about 10hours straight and my initial fears were unfounded, its actually made the animation stuff i was coding unbelievably simple.

    Future updates with more api access will be great but for now, jaw is on floor with awesomely awesomeness that is mecanim.
    It actually plays real nice with my own controller system and pierrepaul, i had ol' teddy running around in minutes.

    I actually cant believe how simple mecanim was to setup and use. Great job guys and gals!!!

    ps would love access to individual animation clip speeds in blend trees through scripting although i guess setting the global blend tree value is enough for now...wait is that even accesible, i forgot to check?
     
  12. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    The only runtime acces that you have regarding speed now is the animator.speed. We would like to expand that in the future!
     
  13. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150

    That would be good and also an animator string parameter so i could for example have a parameter named land and set it to light, heavy, heavyOnSand etc and choose the correct controller state instead of using an int id.
    This would also allow easier tracking of stuff as in my stateController each state is an gameObject in a layer and in my checks ie a fall, i do a fall distance and surface check, using a string paramter my system can set theland string that way and choose the right land from the fall. Ints/floats would get messy.

    Im currently using a blend tree for emotes and set each emote with a float value between 0.0-1.0 then inscript randomly setting the emoteId parameter and choosing which emote to fire. Works well and easily and keeps states down to a min.

    Mecanim has so many things similar to my own system and i was already blending multiple anims.etc and mecanim seems a doddle as my was not gui driven as of yet.
     
    Last edited: Nov 30, 2012
  14. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Having messed with mecanim alot recently ive found the avatar system great and the root motion beneficial for many scenarios however, and its a big however, the mecanim state system is very basic for my needs and until the api matures i cant really use it. I need to fire animations much in the way the legacy system does, where i get full control and let my own statecontroller run things as it is much more mature and has more control/features.

    Do you guys plan on ebentually allowing full control through scripting without the need for the state machine?
    In my system i have OnChange() : boolean callback where you can enter code to decide if you should change state ie
    Function OnChange() : boolean{
    if ( animation.normalizedTime>= value){
    This.layer.EnterState(newState);
    Return true;
    }
    }

    Then my system fires of OnChangeExit/OnExit then OnEnter for the new state. In this way i can fadeOut/In new animations, fire of sounds, setup vars etc and it allows great control. I can even use multiple layers ie multiple main layers for different state groups ie have jumps>descent>land in its own layer and switch to that layer if a current state in a different layer enters the state in that layer, also firing the old layers OnExit and the new layers OnEnter.. Its a really complex state machine ive design for my character system to allow me to create mario galaxy style games.

    Ps i really want to use mecanim but i feel like im goin backwards in many respects trying to intergrate it.
    End of rant. Sorry guys. Mecanim is awesome!
     
  15. pierrepaul

    pierrepaul

    Unity Technologies

    Joined:
    Jun 19, 2012
    Posts:
    162
    Hi. I can understand that if you already have a SM controller it might be costy to change system.

    We have a couple things that we definitely be looking into in the next months, no promises here, but a couple things we are investigating :

    * give full runtime control of the StateMachine ( force a State/Transition etc..)
    * give the ability to override StateMachine results ( so you could change the animation weights, and maybe hack in other animations...)
    * give the ability to create all mecanim assets via API

    But for now, with what we have, I think we can already solve a couple of your problems :

    This is exactly what a Condition in a Transition do. Create a transition and set an ExitTime condition.

    We don't currently offer that natively ( and we are having internal discussion whether we should or not do this). That being said, we publish enough hooks to the Animator ( animator.IsInTransition(layerIndex), animator.GetCurrentAnimatorStateInfo(0) ) that I think it would be relatively easy for users to have a tiny logic layer that manages that. I have seen a couple production do exactly that!

    pp
     
  16. TheSin

    TheSin

    Joined:
    Aug 4, 2011
    Posts:
    150
    Thankyou again for replying.

    I understand mecanimis in ots infancy and can only imagine where its heading.

    Regarding the OnChange callback, i understand i can do that basic example through parameters but what if i want to add in collision data, velocity plus a whole lot more, then based on results jump straight to a state based on that result. It gets messy fast as and currently id have to workaround mecanim in a very limited way.

    Its good to hear the ideas you guys have and i have afew of my own im currently using. I posted in another thread that itd be great if we could just attach monbehaviours to states with callbacks. Its how im currently doing my stuff, and by no means am i as crazily smart as you guys, i actually think it makes it a whole lot simpler and more powerful. I guess once we get more access to the api this will be easier to put in ourselves. Hopefully.

    Ps hope i dont sound critical, just finding mecanims state control hard to work with. I currently have characters running around doing crazy stuff and can simply plug in or out new states/moves and simply add in a little OnChange() callback to go from a state to that one. Sorry i do tend to rant!