Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

play animation on key press help

Discussion in 'Scripting' started by phil.cooper@mtcl.net, Jun 16, 2010.

  1. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    i am currently using the following script to control my character. At the moment the 'crouch' animation is played when the 'c' button is held down, i need to change it so the animation plays when the 'c' button is pressed instead of held down.

    does anyone know how to do this?

    Code (csharp):
    1. //animation["CROUCH"].layer=1;
    2.  
    3. function Update ()
    4. {
    5.    if (Input.GetAxis("Vertical") > 0.2){
    6.        animation.CrossFade ("WALK");
    7.        
    8.    }else if(Input.GetAxis("Vertical") < -0.2){
    9.        animation.CrossFade ("REVERSE");
    10.        // Play animation backwards when running backwards
    11.       animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
    12.        
    13.    }else{
    14.       animation.CrossFade ("IDLE");
    15.    }
    16.    
    17.     if(Input.GetKey("c")){
    18.        animation.CrossFade ("CROUCH");
    19. }
    20. }
     
  2. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    any help would be great
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There's an Input.GetKeyDown function that works like GetKey, but only registers the keystroke on the first frame it happens.
     
  4. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    i replaced 'GetKey' with 'GetKeyDown' as you said, but now the 'crouch' animation doesnt play at all.

    is there something im doing wrong?
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Yes, sorry - I've just spotted what is wrong with that. The CrossFade function needs to be called each frame, so the GetKeyDown call needs a bit of extra work. If you set a boolean variable when the key is pressed, you can test for it as long as you need to run the crouch animation:-
    Code (csharp):
    1. var crouching: Boolean;  // Declared at top of script.
    2.    ...
    3.  
    4. if(Input.GetKey("c")){
    5.    crouching = true;
    6. }
    7.  
    8. if (crouching) {
    9.        animation.CrossFade ("CROUCH");
    10. }
     
  6. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    hi,

    i have altered the script to include what you have said so the script looks like this -

    Code (csharp):
    1. //animation["CROUCH"].layer=1;
    2. var crouching: Boolean;
    3.  
    4. function Update ()
    5. {
    6.    if (Input.GetAxis("Vertical") > 0.2){
    7.        animation.CrossFade ("WALK");
    8.        
    9.    }else if(Input.GetAxis("Vertical") < -0.2){
    10.        animation.CrossFade ("REVERSE");
    11.        // Play animation backwards when running backwards
    12.       animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
    13.        
    14.    }else{
    15.       animation.CrossFade ("IDLE");
    16.    }
    17.    
    18.     if(Input.GetKey("c")){
    19.    crouching = true;
    20. }
    21.  
    22. if (crouching) {
    23.        animation.CrossFade ("CROUCH");
    24. }
    25. }
    but now it is coming up with the error which i dont understand -

     
  7. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    Try "boolean", lowercase "b".
     
  8. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    that got rid of the error message, but now the 'crouch' animation plays every 5 seconds without having to press the button, and the 'walk' animation doesnt work.

    this is doing my head in.
     
  9. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    Seems like you need to set the "crouching" variable to false at some point. If not, once it becomes true, it will remain true forever.
     
  10. deadManN

    deadManN

    Joined:
    May 12, 2010
    Posts:
    154
    in here:
    if (crouching) {
    animation.CrossFade ("CROUCH");
    }

    define variable and check if it become true again and set it to false after once script ran, and turn the value back when u walk....

    so for next chouch u can use that again... so till you don't walk the variable have false value(after 1 shot runing) and don't allow your char to do chouching again
     
  11. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    could someone post some code which would fix this? im very new to javascript and not to sure what im doing.
     
  12. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    In your code, I don't see anywhere that you put crouching back to false.

    You initialize it as false, then, when the user hits "c" you set it to true, but then it never goes back to false.

    So you should set it to false at some point...

    Or, do not use a crouching variable and go with the deadManN suggestion.

    Replace this:

    Code (csharp):
    1.  if(Input.GetKey("c")){
    2.    crouching = true;
    3. }
    with what deadManN suggested:

    Code (csharp):
    1. if (crouching) {
    2. animation.CrossFade ("CROUCH");
    3. }
     
  13. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    i think ive done what you said but now he doesnt crouch, can you see what im doing wrong?

    Code (csharp):
    1. //animation["CROUCH"].layer=1;
    2. var crouching: boolean;
    3.  
    4. function Update ()
    5. {
    6.    if (Input.GetAxis("Vertical") > 0.2){
    7.        animation.CrossFade ("WALK");
    8.        
    9.    }else if(Input.GetAxis("Vertical") < -0.2){
    10.        animation.CrossFade ("REVERSE");
    11.        // Play animation backwards when running backwards
    12.       animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
    13.        
    14.    }else{
    15.       animation.CrossFade ("IDLE");
    16.    }
    17.    
    18. if (crouching) {
    19.        animation.CrossFade ("CROUCH");
    20. }
    21. }
     
  14. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    My bad.

    Use this instead:

    Code (csharp):
    1.  
    2. if(Input.GetKey("c")){
    3.    animation.CrossFade ("CROUCH");
    4. }
    5.  
     
  15. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    thats taken it back to my origional problem, the crouch animation is played when the key is held down. I need it to play in full when the key is pressed.
     
  16. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    then you need to set your CROUCH animation to WrapMode.Once and use something like this:

    Code (csharp):
    1.  
    2. if(Input.GetKey("c"))
    3. {
    4.        animation.CrossFade ("CROUCH");
    5.  
    6.        while(animation.isPlaying)
    7.             yield;
    8. }
    Which will cause problems in a Update function though, because of the yield (or maybe not in javascript? I dont really use javascript)
     
  17. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    You should probably use the layer property of the animation state.

    You should set the crouching animation in a bigger layer than the idle.

    Therefore, while the crouching animation is playing, the idle will not play. But as soon as the crouching stops, idle will start.

    Check the documentarion regarding Layer in the AnimationState.
     
  18. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    i am already using the layer function -

    Code (csharp):
    1. //animation["CROUCH"].layer=1;
    2. var crouching: boolean;
    3.  
    4. function Update ()
    5. {
    6.    if (Input.GetAxis("Vertical") > 0.2){
    7.        animation.CrossFade ("WALK");
    8.        
    9.    }else if(Input.GetAxis("Vertical") < -0.2){
    10.        animation.CrossFade ("REVERSE");
    11.        // Play animation backwards when running backwards
    12.       animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
    13.        
    14.    }else{
    15.       animation.CrossFade ("IDLE");
    16.    }
    17.    
    18. if(Input.GetKey("c")){
    19.    animation.CrossFade ("CROUCH");
    20. }
    21. }
    any other ideas?
     
  19. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    In your code, the layer sentence is commented out.

    Also, you're using 1 (one) as a value for the crouch layer. I would suggest using something higher, like 2 or 3. Since 1 might be the default (not sure about this, but just to be safe).

    And if you want to be extra careful, also set the layers of the walk and reverse animation to a lower value, and the idle to an even lower value.

    I would use:
    idle at layer -5
    walk / reverse at layer -1
    crouch at layer 2
     
  20. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    THANKS, THATS WORKED PERFECTLY!
     
  21. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    not quite.

    now the player is constantly playing the walk animation when i press the key to walk. and not stopping when i release they key.

    what could be causing this?
     
  22. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    I would really appreciate some help with this.
     
  23. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    You're not stopping the walk animation when you release the key.

    Code (csharp):
    1. if ( Input.GetKey( WalkKey ) ) {
    2.   // walk
    3. } else {
    4.   // Blend walk to zero influence over 0.2 seconds
    5.   animation.Blend("walk", 0, 0.2);
    6. }
     
  24. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    Hi,

    ive ammended my script to this -

    Code (csharp):
    1. animation["WALK"].layer=-1;
    2. animation["REVERSE"].layer=-1;
    3. animation["CROUCH"].layer=2;
    4. animation["IDLE"].layer=-3;
    5. var crouching: boolean;
    6.  
    7. function Update ()
    8. {
    9.    if (Input.GetAxis("Vertical") > 0.2){
    10.        animation.CrossFade ("WALK");
    11.        
    12.     }else{
    13.   // Blend walk to zero influence over 0.2 seconds
    14.   animation.Blend("WALK", 0, 0.2);
    15.   }
    16.    else if(Input.GetAxis("Vertical") < -0.2){
    17.        animation.CrossFade ("REVERSE");
    18.        // Play animation backwards when running backwards
    19.       animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical"));
    20. }
    21.   else{
    22.       animation.CrossFade ("IDLE");
    23.    }
    24.    
    25. if(Input.GetKey("c")){
    26.    animation.CrossFade ("CROUCH");
    27. }
    28. }
    but it is coming up with the errors -

    not sure how to fix it