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 MSB · Apr 11, 2014 at 05:00 PM · buttonmecanimissueinput.getaxis

Bizarre non-responsive button/Mecanim issues

Heya UnityAnswers long time no see.

I'm trying to get my character too animate using Mecanim. The animations will work depending on the position of the player and is controlled at the moment with the WSAD keys being Up, Down, Left, Right.

I've got this bit of code in the update to deal button press/movement

     float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; //sets left/right to arrow keys * game speed
     float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;    //sets up/right to arrow keys * game speed
         transform.Translate(h, v, 0); //Will cause the player too move
         transform.position = new Vector3(Mathf.Clamp(transform.position.x, -13f, 13f), Mathf.Clamp(transform.position.y, 0.5f, 16f), transform.position.z); //Prevents player going off screen


I've got parameters set up in Mecanim like so -

alt text

Then I have this code too control the left and right blending

                 if(Input.GetAxis("Horizontal") < 0)
     {
         if(Input.GetKey("a"))
         {
         animator.SetBool("Left", true);
         animator.SetFloat("MoveX", transform.position.x);
     
         }
         
         if(Input.GetKeyUp("a"))
         {
         animator.SetBool("Left", false);
         animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
         
         }
     }
     
     if(Input.GetAxis("Horizontal") > 0)
     {
         if(Input.GetKey("d"))
         {
         animator.SetBool("Right", true);
         animator.SetFloat("MoveX", transform.position.x);
         
         }
         
         if(Input.GetKeyUp("d"))
         {
         animator.SetBool("Right", false);
         animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
         }
     }


This will now at least change the "MoveX" Float in Mecanim, when I press the a or d button I see the value shift according to the "transform.position.x" and the Bools turn True and False correctly.

How ever when I try to do the same with Up and Down...

         if(Input.GetAxis("Vertical") < 0)
         {
             if(Input.GetKey("w"))
             {
             animator.SetBool("Up", true);
             animator.SetFloat("MoveY", transform.position.y);
             
             }
             
             if(Input.GetKeyUp("w"))
             {
             animator.SetBool("Up", false);
             animator.SetFloat("MoveY",  transform.position.y, 0, Time.deltaTime);
             }
         }
         
             
         if(Input.GetAxis("Vertical") > 0)
         {
             if(Input.GetKey("s"))
             {
             animator.SetBool("Down", true);
             animator.SetFloat("MoveY", transform.position.y);
             
             }
             
             if(Input.GetKeyUp("s"))
             {
             animator.SetBool("Down", false);
             animator.SetFloat("MoveY",  transform.position.y, 0, Time.deltaTime);
             }
         
         }

The "MoveY" float won't change, infact if even if I just use

         if(Input.GetAxis("Vertical") < 0)
         {
             if(Input.GetKey("w"))
             {
             Debug.Log("W HELD DOWN");
             
             }
             
             if(Input.GetKeyUp("w"))
             {
             Debug.Log("W LET GO");
             }
         }

...the console won't even log the W button being pressed and the only way I can get it to notice it has, is if I'm holding down the S button first, like it's waiting for that button to be held down before printing "W HELD DOWN"

So that's the first issue, why aren't the W/S buttons working like the A/D buttons? It's the same code but the W/S buttons won't respond unless I'm holding down the other button first (Pressing W will only change "MoveY" if S is being held down first and vice versa)

The 2nd far less important issue is I thought that using...

     if(Input.GetKeyUp("d"))
             {
             animator.SetBool("Right", false);
             animator.SetFloat("MoveX",  transform.position.x, 0, Time.deltaTime);
             }


..would mean that once the D button has been lifted then "MoveX" will smoothly transition from the current "transform.position.x" to zero.

Any ideas why that the W/S buttons are acting so strange despite being the same as the A/D buttons? does it have something to do with the clamping?

p.jpg (38.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mikael-Gyth · Apr 11, 2014 at 05:25 PM

in this code

 if(Input.GetAxis("Vertical") < 0)
        {
          if(Input.GetKey("w"))
          {
          Debug.Log("W HELD DOWN");
  
          }
  
          if(Input.GetKeyUp("w"))
          {
          Debug.Log("W LET GO");
          }
        }

you are saying if VerticalAxis is less than zero and the key is W then say "W HELD DOWN"

the problem is that VerticalAxis will return a positive number when W is pressed. So if evaluation will fail. If hovever you press S, then VerticalAxis will return -1 and the succeding W press will be caught and "W HELD DOWN" will be printed.

I would suggest that you read up a bit on how axis works and how you can use them to control mechanim. It seems to me that you are overcomplicating matters a bit.

You should be able to send the axis directly to the animator like this:

_animator.SetFloat("Vertical", VerticalAxis);

if you then set up your blendtree to choose backward or foreward movement based on the Vertical parameter beeing a positive or a negative, and going to idle if it's 0 you should be good to go.

Comment
Add comment · Show 2 · 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 MSB · Apr 11, 2014 at 05:31 PM 0
Share

Thanks, yeah I was just doing some testing and it worked fine when I took the button presses outside of the Input.GetAxis function so I'm going to explore that further.

avatar image MSB · Apr 11, 2014 at 05:35 PM 0
Share

Ah cheers I'll have a look into that.

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

22 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

Related Questions

Generic USB Controller axis issue 0 Answers

Mecanim tutorial problem 0 Answers

How to animate a UI Button without using Mecanim? (For example, by using DOTween instead) 1 Answer

iTween.ValueTo doesn't work? 0 Answers

I'm having trouble loading a scene after the Roll-A-Ball example project. 0 Answers


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