Search Unity

split animation states from logic states?

Discussion in 'Scripting' started by mahdiii, Oct 21, 2017.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Hi. Suppose we have some logical states (chase,move to a random checkpoint etc) and have some animation states (idle,run,walk,crouch).
    we know you can be in chase state and also run/walk/crouch or for move to a random checkpoint etc
    Is it better to separate them from each other?
    mechanim for animation states and state machine design pattern for logical states?

    Code (CSharp):
    1. public class CChase:IState{
    2.    public void Start(){
    3.  
    4.    }
    5.    public void Update(){
    6.       if(...){
    7.           // animator.setFloat("",);
    8.       }
    9.       else{
    10.           // animator.setFloat("",);
    11.       }
    12.    }
    13.    public void Exit(){
    14.  
    15.    }
    16. }
     
    Last edited: Oct 21, 2017
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Mechanim is a state machine and some people use it for general ai:
    https://medium.com/the-unity-develo...ow-to-repurpose-unity-s-animator-7c6c421e5785

    However, I am still a little wary of it because it was actually created for animation states. I'm still up in the air on it, though. There are examples on youtube, but they seem pretty simplistic. I think it would require breaking the code down into smaller blocks and might make it overly complicated.

    I might do a tutorial on it and play with it to see if it would be useful enough.
     
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    So you say create all states in mechanim?! (chase,move to a random checkpoint etc (that are logical) and run/walk/crouch (that are animations))
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yeah, you can because there is a StateMachineBehaviour class in Unity that will show up in Mecanim, so you can do logic with it.
    https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours
    That's my understanding of it, but I haven't had time to really check it out.
     
  5. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I know state machine behaviour but I think you didn't know my question
    "we know you can be in chase state and also run/walk/crouch or for move to a random checkpoint etc"
    they are independent so needs to create two separate state machines
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yes, I'm just saying they could be combined by using mecanim for logic. Animations, though, are really separate things, like sound effects or something. They are only states in a smaller sense. Your code would be a mess if walking was a state. I wouldn't treat them as a state myself. You might walk while patrolling, but that's a very small part of the logic. The real logic is moving between destinations and wating for other input. They are closely linked though. Usually you walk while patrolling, but chasing might involve hiding behind objects, occasional shooting, etc. Some people like to break it down to it's smallest part, but there has to be some motive for walking. I think you would almost need substates, also. Like stopping to shoot while chasing would be a substate. When finished you might look for other cover, or direct chase, or whatever, but I don't think it would ever get down to just walking myself because it is always connected to a larger motivation.
     
    Last edited: Oct 22, 2017
    mahdiii likes this.