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

Toggle 1 singe key with animation = rocket science?

Discussion in 'Scripting' started by cruising, Jul 21, 2014.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have tried a lot of different codes but no one does the task. The layer just do the animation 1 sec and stands up right after pressing E
    Mostly of the codes is similar to this one, and this one also do not work;
    Code (JavaScript):
    1. #pragma strict
    2. var changeToggle : boolean;
    3.  
    4. function Update ()
    5.     {
    6.     if (Input.GetKeyDown(KeyCode.E)){
    7.     changeToggle = !changeToggle;
    8.    
    9.     if (changeToggle == true) {
    10.     animation.Play("sneak_idle");
    11.    
    12.     }else {
    13.    
    14.     animation.Stop("sneak_idle");
    15.    
    16.         }
    17.     }
    18. }
    Even this one doesnt work
    Code (JavaScript):
    1.  
    2.     var PlayAim = false;
    3.    
    4.     function Update() {
    5.     if (Input.GetKeyDown(KeyCode.E)){
    6.     if (PlayAim == false) {
    7.     PlayAim = true;
    8.     animation.Play("sneak_idle");
    9.     }
    10.     else { // else PlayAim is true
    11.     PlayAim = false;
    12.     animation.Stop("sneak_idle");
    13.     }
    14.     }
    15.     }
    Can anyone help me solve this please?
     
    Last edited: Jul 21, 2014
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    GetKeyDown is only true for the frame it's pressed so you are effectively calling the animation in one frame and stopping it in the next frame. try removing / commenting out your else statement to make sure it plays as it should and go from there.
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    If i remove so i only have this
    Code (JavaScript):
    1.    
    2.         var PlayAim = false;
    3.      
    4.         function Update() {
    5.         if (Input.GetKey(KeyCode.E)){
    6.         if (PlayAim == false) {
    7.         PlayAim = true;
    8.         animation.Play("sneak_idle");
    9.         }
    10.     }
    11. }
    The player just do the animation 1 sec and stands up, it doesnt hold sneak position.
     
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Set the animation to loop / clamp forever(I think it's called) and it'll keep playing.

    Are you trying to do a toggle or only with the key held down? EDIT: re-read the title and it says toggle so:

    Code (csharp):
    1.  
    2. // code is unityscript... not sure how to change the code type in the code tag *smack head*
    3. var playAnim:bool = false;
    4.  
    5. function Update(){
    6.      if(Input.GetKeyDown(KeyCode.E){
    7.           playAnim!=playAnim;
    8.           ToggleAnim();
    9.      }
    10. }
    11.  
    12. function ToggleAnim(){
    13.      if(playAnim){
    14.           animation.Play("sneak_idle");
    15.      }
    16.      else{
    17.           animation.Stop("sneak_idle");
    18.      }
    19. }
    20.  
    21.  
     
    Last edited: Jul 21, 2014
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Im trying to toggle E so i dont need to push 3 buttons when sneak run. Toggle E for sneak_idle, and then if you press W you sneak_walk, and if you press Ctrl + W button you sneak_run. I have tried that clamping but didnt get it to work
    EDIT: tried your code, getting 1 error ""The name 'bool' does not denote a valid type ('not found')""
    for the "var"

    EDIT": i had to make some changes to get it to work, but still the player just do the animation 1 sec and then stands up
    Code (JavaScript):
    1. var playAnim : boolean = false;
    2. function Update(){
    3.      if(Input.GetKeyDown(KeyCode.E)){
    4.           playAnim = !playAnim;
    5.           ToggleAnim();
    6.      }
    7. }
    8. function ToggleAnim(){
    9.      if(playAnim){
    10.           animation.Play("sneak_idle");
    11.      }
    12.      else{
    13.           animation.Stop("sneak_idle");
    14.      }
    15. }
     
    Last edited: Jul 21, 2014