Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Player State & Animator - Help Needed!

Discussion in 'Animation' started by CelticKnight, Feb 26, 2015.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hello guys,

    I've run into a problem - and I don't know what the solution is. And it's a bugger to describe the problem.
    The question is like this, how do I get control of the player animations to match the "player states" if you will? I can't descibe it better than that.

    I have a basic, and I mean *really* basic project in which I am trying to figure out the nuts and bolts of an FPS. So, far I have the idle state working correctly, the walk states are all working off it, but, I cannot get the down/backward running state to show as i intend. It probably looks very clumsy but it the only thing I could come up with.



    The conditional for gun_down -> gun_run_down in the Animator is below:


    The code is as follows - it's long winded for clarity to me and error-checking and using the console:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //attach script to the object that we want to apply the ""Change State"
    4. //this is done by going to inpector for that object clicking on
    5. //"Add Component" button at bottom of object -> Scripts then
    6. //finding appropriate Script - ie "Change^State"
    7.  
    8. //Script to change state from idle to walk -> maybe run eventually
    9. //script modified slightly - emphasis on slightly - from YouTube tutorial
    10. //on mocap scripting
    11.  
    12. //doesn't really matter if internal or not
    13. internal var anime: Animator;
    14.  
    15. //vars to get vertical horizontal movement
    16. var v:             float; //vertical up/down arrows
    17. var h:             float; //horzizontal (strafing) movement
    18. var sprint:     float; //set value for sprinting
    19. //boolean values for me to play with
    20. var isDown:        boolean;
    21. var isSprint:     boolean;
    22. var DoubleDown:    boolean;
    23.  
    24.  
    25. //get access to the animator
    26. function Start () {
    27.     //
    28.     anime = GetComponent(Animator);
    29.     }//-->end Start function
    30.  
    31.  
    32. //get vertical and horizontal vars
    33. function Update () {
    34.     //print("sprint: "+sprint);
    35.     v = Input.GetAxis("Vertical");
    36.     h = Input.GetAxis("Horizontal");
    37.     //r = Input.GetAxis("Sprinting");
    38.     sprinting();
    39.     //print("sprint: "+sprint);
    40.  
    41.     if(v < -0.1){
    42.         isDown = true;
    43.         print("Direction: down: "+isDown);
    44.         }
    45.     else{
    46.         isDown = false;
    47.         print("Direction not!: "+isDown);
    48.         }
    49.  
    50.     if(isDown && isSprint){
    51.         //both conditions are met
    52.         DoubleDown = true;
    53.         print("Both conditions are met");
    54.         }
    55.     else
    56.         DoubleDown = false;
    57.  
    58.     print("DoubleDown: " + DoubleDown);
    59.  
    60.     }//-->end Update function.
    61.  
    62.  
    63.  
    64. //set the variable in the Animator to play correct animation
    65. function FixedUpdate(){
    66.     anime.SetFloat("Walk",v);
    67.     anime.SetFloat("Str",h);
    68.     anime.SetFloat("Running",sprint);
    69.  
    70.     anime.SetBool("DDown", DoubleDown);
    71.  
    72.     //should horizontal/strafing movement be used in access walk animation or
    73.     //something different ?!?!?
    74.  
    75.     }//-->End FixedUpdate function
    76.  
    77.  
    78. function sprinting(){
    79.     if(Input.GetButton("Sprinting")){
    80.         isSprint = true;
    81.         sprint = .2;
    82.         }
    83.     else{
    84.         isSprint = false;
    85.         sprint = 0.0;
    86.         }
    87.     //print("Sprinting........................");
    88.     }//-->End sprinting function

    So, I must be not understanding how the SetBool value is sent and applies it to the Animator. The way I figure it the boolean value DoubleDown is true, when both conditions are met, so:

    "if DDown = true", which is does, according to the code, then it should play the next animation in the chain. So what am I not getting here?

    And on top of all that I need to another animations for gun-aiming (ie down the scope) when Idle/Walking or both and I don't have a clue how to do this! This Animator thing has turned my mind to mush.

    So any help at all would be greatly appreciated :).

    Cheers.
     
  2. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Is there any other information I can supply to help get an answer?
     
  3. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    ok, I'll have a stab at it. I guess gun_run_down means 'running while gun pointing down'.
    1. the gun_run_down state only comes from idle state. I think the walk and run states should come from idle, and gun up or down should come from walk/run states.
    2. I think you'd be better off with two layers, one for upper body and one for lower body. then on lower body layer you'd have idle/walk/run and upper body layer gundown/gunup/aimleft/aimright. for the latter two you can use blend trees (maybe for the first two also)
     
  4. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    That's basically right - good guess from my pretty cryptic naming :p.

    So the gun-run-down state in my example doesn't really have anything to do with the gun_walking state at all?

    After reading your reply, I managed to get the idle-> walk states through a 2D simple blend tree with "idle" in the middle, and walk for Up/Down/Left and Right directions around that, don't really understand "why" that works!?! And then managed to get a run state tacked onto that, that goes in the main directions also.

    I suppose I'll try making a blend tree for the aiming as well and see how much I can stuff it up :).

    I really like the idea of two layers as well when I get more proficient I'd like to try that :cool:.