Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexjadkins · May 04, 2020 at 09:55 AM · animationscripting beginnerblend treeinput.getkey

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);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

319 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sometimes blend tree do not work 1 Answer

Time.deltaTime is not smooth 1 Answer

Blend tree preview doesn't update until past left diamond 0 Answers

How can I play a specific animation upon a combination of inputs using the Blend Tree? 1 Answer

U4.3.4: Mecanim blend trees, no more old docs? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges