- Home /
Input stops being transmitted after pressing keys multiple times
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:
public class BasicMovement : MonoBehaviour {
public Animator animator;
float lastX, lastY;
// Update is called once per frame
void Update()
{
//if the user hits shift, isRoll is set to true, triggering the roll blend tree
if(animator.GetBool("isRoll")==false && Input.GetKey(KeyCode.LeftShift)){
animator.SetBool("isRoll", true);
}
//if the user hits e, IsSlap is set to true, triggering the slap blend tree, uses .GetKeyDown to prevent holding
//down e to continously slap
else if(animator.GetBool("isRoll")==false && Input.GetKeyDown(KeyCode.E)){
animator.SetBool("IsSlap",true);
}
//Checks if rolling or slapping, otherwise move goes as normal
if(!animator.GetBool("isRoll") && !animator.GetBool("IsSlap"))
Move();
//Case: slap, you can move and slap at same time
else if(!animator.GetBool("isRoll") && animator.GetBool("IsSlap")){
Slap();
Move();
}
//case: roll, you can move and roll at same time but moving while rolling is handing in roll function
else if(animator.GetBool("isRoll"))
Roll();
}
//Movement function, if the keys are let go then the last horizontal and vertical values are stored so the
//idle blend tree can play the correct directional idle animation
void Move(){
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
transform.position += movement * Time.deltaTime;
if(!Input.anyKey){
animator.SetFloat("LastHorz", lastX);
animator.SetFloat("LastVert", lastY);
animator.SetBool("Movement",false);
}
else if(!Input.GetKey(KeyCode.E)){
lastX = movement.x;
lastY = movement.y;
animator.SetBool("Movement", true);
}
}
void Roll(){
//if rolling animation has finished, isRoll is set to false so animation returns to the running blend tree
if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0))
animator.SetBool("isRoll",false);
//Otherwise the rolling animation is still playing, so the movement is taking from directional Input
//if no directional input is put in, then the player still moves in the last direction used until animation is over
else{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") > 0){
movement.x += .5f;
movement.y += .5f;
}
else if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") == 0)
movement.x += .5f;
else if(animator.GetFloat("Horizontal") > 0 && animator.GetFloat("Vertical") < 0){
movement.x += .5f;
movement.y -= .5f;
}
else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") > 0){
movement.x -= .5f;
movement.y += .5f;
}
else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") == 0)
movement.x -= .5f;
else if(animator.GetFloat("Horizontal") < 0 && animator.GetFloat("Vertical") < 0){
movement.x -= .5f;
movement.y -= .5f;
}
else if(animator.GetFloat("Horizontal") == 0 && animator.GetFloat("Vertical") < 0)
movement.y -= .5f;
else if(animator.GetFloat("Horizontal") == 0 && animator.GetFloat("Vertical") > 0){
movement.y += .5f;
}
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
transform.position += movement * Time.deltaTime;
}
}
//Once slap animation is finished, isSlap is set to false, returning back to either the running blend tree or idle blend tree
void Slap(){
if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 && !animator.IsInTransition(0))
animator.SetBool("IsSlap",false);
}
}