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 /
This question was closed Jan 18, 2018 at 12:39 PM by dee2061 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by dee2061 · Jan 17, 2018 at 10:12 AM · windows 10input.getkey

Double key combination problem

So here is my problem. I'm writing an animator controller and I'm trying to use a combination of two keys for sprinting animation. Thing is they work only one way, for example if I press W and then LeftShift, everything is just peachy, but when I do the presssing in a reversed order, it just doesn't work. Here's the code:

  if (Input.GetKey (KeyCode.LeftShift)) {
                     if (Input.GetKey (KeyCode.W)) {
                         anim.SetBool ("isWalking", true);
                         anim.SetBool ("IsRunning", true);
                         Debug.Log ("isWalking = true");
                         Debug.Log ("IsRunning = true");
                     } else {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("IsRunning", false);
                         Debug.Log ("isWalking = false");
                         Debug.Log ("IsRunning = false");
                     }
     } else {
                     if (Input.GetKey (KeyCode.W)) {
                         anim.SetBool ("isWalking", true);
                         anim.SetBool ("IsRunning", false);
                         Debug.Log ("isWalking = true");
                         Debug.Log ("IsRunning = false");
                     } else {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("IsRunning", false);
                         Debug.Log ("isWalking = false");
                         Debug.Log ("IsRunning = false");
                     }
     
     }

I know that the code is rather long and one if clause would do the trick because I already tried that. But I had the same problem back then and I thought that maybe it's a compilator issue with the order of conditions inside the if clause. That's why I arrived with this solution that doesn't help either. On a side note, I have the animator set up the way that you need both isWalking and isRunning conditions to be true to get the running animation.


So an example of how this works upon testing: I press down W, the character walks, while still pressing W i press down the LEFT SHIFT key, the character runs. I let go of the LEFT SHIFT key, character walks again, I press LEFT SHIFT again, the character runs again. But when I either start with LEFT SHIFT key or let go of W while running and then press W again the animations do not play. It's not a key issue, because I tried using different keys instead of LEFT SHIFT. Funny thing is, I get the Debug.Log ("isWalking = true") and Debug.Log ("IsRunning = true") inside the console. Any suggestions?

Comment
Add comment · Show 3
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 Harinezumi · Jan 17, 2018 at 11:19 AM 1
Share

The code seems to be correct, I have a feeling it is an issue with how the animation controller is set up. Does the running state depend on the walking state or reverse? In that case it is possible that the animation controller does not detect the change of both variables at the same time. You can look at the animation controller at run-time and see into what state it wants to go.
(I'm pretty sure it is an animation controller logic issue, because the only difference between pressing W first or L-shift is that in the second case both variables change at the same time)

avatar image Bunny83 Harinezumi · Jan 17, 2018 at 01:30 PM 0
Share

Yep that most likely is the problem.

avatar image meat5000 ♦ · Jan 17, 2018 at 01:50 PM 0
Share

Observing your state machine whilst it is running should point to the issue. I think it is probably something to do with how the transition conditions are configured and the flow of the States.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by dee2061 · Jan 18, 2018 at 12:38 PM

Yeah, it turned out that I messed up transitions from idle to running, that's why idle to walking was working, and walking to running was ok too. THX for your help.

Comment
Add comment · Share
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
0

Answer by Diukrone · Jan 17, 2018 at 06:58 PM

//Pseudo code, maybe it can help!

 if (Input.GetKey (KeyCode.LeftShift) && Input.GetKey (KeyCode.W)) 
 {
    if(anim.SetBool ("isWalking") == false && anim.SetBool ("isRunning") == false)
    {
          anim.SetBool ("isWalking", true);
          anim.SetBool ("isRunning", true);
    }
     else 
   {
 
  anim.SetBool ("isWalking", false);
          anim.SetBool ("isRunning", false);  
    }
 }
 else
 {
      if (Input.GetKey (KeyCode.W)) 
                      {
                          anim.SetBool ("isWalking", true);
                          anim.SetBool ("IsRunning", false);
 
                      } 
                    else 
                        {
                          anim.SetBool ("isWalking", false);
                          anim.SetBool ("IsRunning", false);
                      }
 
 }
 }
 
 
 
 
 

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

79 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

Related Questions

making obgect invisble then visible 1 Answer

How to get constant rotation from tapping a button 1 Answer

Playing / Stoping sound in C# 1 Answer

Unity resolution changed since I update my pc on Windows 10 1 Answer

Does Unity Web Player works on Windows 10 x64? 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