Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 rollingscissor · May 31, 2015 at 12:27 PM · animationmovementinputplatformer

How Do I Check if another key was pushed while one key was being held down?

I have animations and movement in my game rigged up to the same key presses. I need to have the animation of walking to the right change to walking to the left on a given key press. However, when I try this, if the right key is being held down, it doesn't register that the left key was pushed and is now being held down. So I can essentially be holding left, or right, and my character will walk in the opposite direction. The code is the unholy abomination below.`using UnityEngine; using System.Collections;

public class Player : MonoBehaviour { public Animator PlayerAnimator; public float moveHorizontal; public float speed; public SpriteRenderer PlayerRenderer; public Sprite[] PlayerSprites;

 void Start () 
 {
    
 }
 
 
 void Update () 
 {
     PlayerMovement();
 }
 
 void PlayerMovement()
 {
     if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
     {
         moveHorizontal = -1f;
         PlayerAnimator.SetTrigger("PlayerWalkLeft");
     }
     else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
     {
         moveHorizontal = 1f;
         PlayerAnimator.SetTrigger("PlayerWalkRight");
     }
     /*****************/
     /******************************************************************************/
     if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.RightArrow))
     {
         moveHorizontal = 0f;
         PlayerAnimator.SetTrigger("PlayerLeftFacingIdle");
     }
     else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftArrow))
     {
         moveHorizontal = 0f;
         PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
     }
     /*************************************/
     /****************************************************/
     if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
     {
         if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
         {
             moveHorizontal = 1f;
             PlayerAnimator.SetTrigger("PlayerWalkRight");
         }
     }
     else if(Input.GetKey(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
     {
         if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
         {
             moveHorizontal = -1f;
             PlayerAnimator.SetTrigger("PlayerWalkLeft");
         }
     }

     gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveHorizontal * speed, 0);

 }

}

`

Comment
Add comment · Show 1
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
avatar image hexagonius · May 31, 2015 at 01:56 PM 1
Share

I'd change this a bit:

On one of the key downs, set that direction as the current (by moveHorizontal and playAnimation). On key up, check if the opposite key is still held and set that.

Right now you're kind of overriding things because you're checking held buttons with an if else which gives A precedence over D because of the execution order.

      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
      {
          moveHorizontal = -1f;
          PlayerAnimator.SetTrigger("PlayerWalkLeft");
      }
 else      if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
      {
          moveHorizontal = 1f;
          PlayerAnimator.SetTrigger("PlayerWalkRight");
      }
 else      if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A))
      {
          if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
      {
          moveHorizontal = 1f;
          PlayerAnimator.SetTrigger("PlayerWalkRight");
      }
  {
      moveHorizontal = 0f;
      PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
  }
      }
 else      if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D))
      {
          if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
      {
          moveHorizontal = -1f;
          PlayerAnimator.SetTrigger("PlayerWalkLeft");
      }
  {
      moveHorizontal = 0f;
      PlayerAnimator.SetTrigger("PlayerRightFacingIdle");
  }
      }

There's probably an even better way, but right now I don't have the time to think this through

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Disable Diagonal Movement 1 Answer

Handling input, movement, animation with scripts? 1 Answer

Animation not occuring instant,Survival Shooter Walking animation not instant... 1 Answer

My character is not moving when playing animation 0 Answers

Simple animation script not working (C#) 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