Search Unity

Input stops being received after pressing key multiple times or holding key down

Discussion in 'Scripting' started by alexjadkins, May 3, 2020.

  1. alexjadkins

    alexjadkins

    Joined:
    May 2, 2020
    Posts:
    4
    So I'm working on my first game, and I've encountered a weird behavior that I can't seem to fix. My game is top down 2D sprite based, right now utilizing blend trees for the animations. I have animated my playable character to move in 8 directions. Along with that I've animated 8 directional idle animations.

    I have scripted the ability for my playable character to roll in any of the 8 directions by pressing shift (the character can still control movement during the roll). I've also animated a slap attack (8 directions), which is activated upon pressing e.

    I've found that when I press e, the animation for the slap attack will work as intended most of the time. However, if I hold down e after pressing it and let go, the slap attack animation will finish, the idle animation will then continue, but if I press e again it won't trigger the slap animation. Even watching the animation tree firing, it doesn't even attempt to switch to the slap animation blend tree. However if I move the character at least a few pixels the slap animation will play again after pressing e. It seems that holding down the input button will somehow stop the script from receiving input/playing the slap animation until I move the character again. I can only assume that it's an issue with how I've coded the movement, given how the Input calls are related to the frame rate.

    The issue happens as well with rolling, however not as frequent. The button I've assigned rolling is left shift, and sometimes when I hit shift after continuously moving for awhile the character will pause in place(while still playing the walk animation) until I let go of shift, then the character will continue moving. If I let go of all input and then resume moving, hitting shift will work as intended and commence the rolling animations

    Basically, sometimes the input stops working until I let go of the input keys and then try again.

    Here's my script for the player movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BasicMovement : MonoBehaviour
    6. {
    7.  
    8.     public Animator animator;
    9.     float lastX, lastY;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.  
    15.       //if the user hits shift, isRoll is set to true, triggering the roll blend tree
    16.       if(animator.GetBool("isRoll")==false && Input.GetKey(KeyCode.Space)){
    17.         animator.SetBool("isRoll", true);
    18.       }
    19.       //if the user hits e, IsSlap is set to true, triggering the slap blend tree, uses .GetKeyDown to prevent holding
    20.       //down e to continously slap
    21.       else if(animator.GetBool("isRoll")==false && Input.GetKeyDown(KeyCode.RightShift)){
    22.         animator.SetBool("IsSlap",true);
    23.       }
    24.  
    25.       //Checks if rolling or slapping, otherwise move goes as normal
    26.       if(!animator.GetBool("isRoll") && !animator.GetBool("IsSlap"))
    27.           Move();
    28.  
    29.       //Case: slap, you can move and slap at same time
    30.       else if(!animator.GetBool("isRoll") && animator.GetBool("IsSlap")){
    31.         Slap();
    32.         Move();
    33.       }
    34.       //case: roll, you can move and roll at same time but moving while rolling is handing in roll function
    35.       else if(animator.GetBool("isRoll"))
    36.         Roll();
    37.  
    38.  
    39.  
    40.     }
    41.  
    42.  
    43.  
    44.     //Movement function, if the keys are let go then the last horizontal and vertical values are stored so the
    45.     //idle blend tree can play the correct directional idle animation
    46.     void Move(){
    47.  
    48.       Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
    49.  
    50.       animator.SetFloat("Horizontal", movement.x);
    51.       animator.SetFloat("Vertical", movement.y);
    52.       animator.SetFloat("Magnitude", movement.magnitude);
    53.  
    54.       transform.position += movement * Time.deltaTime;
    55.  
    56.       if(!Input.anyKey){
    57.         animator.SetFloat("LastHorz", lastX);
    58.         animator.SetFloat("LastVert", lastY);
    59.         animator.SetBool("Movement",false);
    60.       }
    61.  
    62.       else if(!Input.GetKey(KeyCode.RightShift)){
    63.         lastX = movement.x;
    64.         lastY = movement.y;
    65.         animator.SetBool("Movement", true);
    66.       }
    67.  
    68.  
    69.   }
    70.  
    71.     void Roll(){
    72.  
    73.       //if rolling animation has finished, isRoll is set to false so animation returns to the running blend tree
    74.       if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0))
    75.         animator.SetBool("isRoll",false);
    76.  
    77.       //Otherwise the rolling animation is still playing, so the movement is taking from directional Input
    78.       //if no directional input is put in, then the player still moves in the last direction used until animation is over
    79.       else{
    80.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
    81.  
    82.         if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") > 0){
    83.           movement.x += .5f;
    84.           movement.y += .5f;
    85.         }
    86.         else if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") == 0)
    87.           movement.x += .5f;
    88.  
    89.         else if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") < 0){
    90.           movement.x += .5f;
    91.           movement.y -= .5f;
    92.         }
    93.         else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") > 0){
    94.           movement.x -= .5f;
    95.           movement.y += .5f;
    96.         }
    97.  
    98.         else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") == 0)
    99.           movement.x -= .5f;
    100.  
    101.         else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") < 0){
    102.           movement.x -= .5f;
    103.           movement.y -= .5f;
    104.         }
    105.         else if(animator.GetFloat("Horizontal") == 0 && animator.GetFloat("Vertical") < 0)
    106.           movement.y -= .5f;
    107.         else if(animator.GetFloat("Horizontal") == 0 && animator.GetFloat("Vertical") > 0){
    108.           movement.y += .5f;
    109.         }
    110.  
    111.         animator.SetFloat("Horizontal", movement.x);
    112.         animator.SetFloat("Vertical", movement.y);
    113.         animator.SetFloat("Magnitude", movement.magnitude);
    114.  
    115.         transform.position += movement * Time.deltaTime;
    116.  
    117.       }
    118.     }
    119.  
    120.     //Once slap animation is finished, isSlap is set to false, returning back to either the running blend tree or idle blend tree
    121.     void Slap(){
    122.       if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0))
    123.         animator.SetBool("IsSlap",false);
    124.  
    125.     }
    126. }
    127.  
     
    Last edited: May 3, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    First, please edit your post and use code tags as per https://forum.unity.com/threads/using-code-tags-properly.143875/ so it is easier to read your code.

    Second, which debugging steps have you tried?
    A few well placed Debug.Log() statements will tell you pretty quickly if the script is receiving input or not. That could quickly narrow your problem down to either an Input problem or an animation problem.
     
  3. alexjadkins

    alexjadkins

    Joined:
    May 2, 2020
    Posts:
    4
    Thanks, just updated the code.
    I'll place some debug.log right now and update.
     
  4. alexjadkins

    alexjadkins

    Joined:
    May 2, 2020
    Posts:
    4
    Yeah so it seems that it still receives input, but the animation refuses to play until I move the character then it will play the animation until it stops again.
     
  5. alexjadkins

    alexjadkins

    Joined:
    May 2, 2020
    Posts:
    4
    I figured it out. My Boolean variable for the slap animation was being set to false immediately after being set to true by the function I have checking when the animation has finished. I assume the animation state machine operates after Update() which would make sense. All I had to do was rearrange my update function to fix this.