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 user5200 · Oct 29, 2012 at 01:09 AM · c#inputgetaxisxboxgetbuttondown

Double tap mechanics and axis inputs

Hello all,

I am making a 2D sprite-based brawler type game, and I am currently encountering some trouble trying to convert some of my keyboard commands to Xbox controller commands which use GetAxis() instead of GetKeyDown() or GetButtonDown().

I already have a working implementation of a double tap to dash mechanic on my main character, however, it does not convert well into the Xbox control scheme.

The problem lies in the fact that I use GetButtonDown() to detect my keyboard inputs while the controller uses GetAxis(). I have solved a bunch of other problems that deal with this sort of issue but I an completely stumped on how I can make a double tap mechanism work on something that doesn't work like GetButtonDown().

For further clarification, this is the root of my problem:

When GetButtonDown() is included in conditionals, it fires once, on the frame that the button press was detected and does not continue firing despite the button being held down. However, Getaxis() acts more like GetButton(), it will fire every single update frame until you let go of the input.

If there is enough interest, I will post the code. Anyone else have similar problems or have helpful tips?

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 MilkX5 · Oct 29, 2012 at 04:29 PM 0
Share

Hi! I'm making a 2d sprite brawler too, however its a standalone game. I'm having trouble making a double-tap dash system for my game. Can you please post the code you used, so I can see where you can change some things? However, there is an alternative that I found. While the walking button is used, another button is pressed to activate the dash.

I can post the code if you want, but this method isn't what I would prefer for my own game. For your problem, I would try using GetButtonUp. It activates when the key is released. Hope that helps, if not I'm really sorry -$$anonymous$$ilk

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Montraydavis · Oct 29, 2012 at 01:27 AM

 var ButtonCooler : float = 0.5 ; // Half a second before reset
 var ButtonCount : int = 0;
 function Update ( )
 {
    if ( Input.GetKeyDown ( KeyCode.X ) ){
       
       if ( ButtonCooler > 0 && ButtonCount == 1){
          //Has double tapped
       }else{
         ButtonCooler = 0.5 ; 
         ButtonCount += 1 ;
       }
    }
 
    if ( ButtonCooler > 0 )
    {
 
       ButtonCooler -= 1 * Time.deltaTime ;
   
    }else{
       ButtonCount = 0 ;
    }
 }

This detects whether or not the player has pressed X twice within 0.5 seconds.

Please forgive me if there are any syntax errors, but, otherwise, this should work just fine for you.

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 user5200 · Oct 29, 2012 at 03:43 AM 0
Share

Hi,

Thanks for trying to help, however, as I have mentioned in my question, I already have working doubletap code for keyboard buttons. And despite the fact that you did not finish reading my post, I tried this, replacing Get$$anonymous$$eyDown() with GetAxis("Horizontal") > 0. It does not work.

I cannot use getkeydown because I am working with GetAxis() on Xbox controllers. I will try to make the main post more clear.

avatar image Montraydavis · Nov 02, 2012 at 01:21 AM 0
Share

You can use this same script with XBOX. You just need to use the correct Input . Just change the Input the the XBOX's configuration ( Which I do not know off the top of my head. ). This should definitely be compatible though .

avatar image
1

Answer by Loius · Oct 30, 2012 at 05:52 PM

Oh, I misinterpreted my brain. Deleted my old answer since it was just wrong anyway. How's this look?

You can leave this running as a coroutine, or just yank out the stuff in the while-loop and put that in an Update - then you can drop the SendMessages too and hooray.

 function TapCheck( axis : String, notify : GameObject ) {
     var lastInput : boolean;
     var tapCount : int;
     var lastTap : float;
     var leeway : float = 0.4; // how fast you have to tap
     var maxtaps : int = 2;
     
     while ( true ) { // contents of this block could go in an Update (sans yield) if desired
         if ( Input.GetAxis(axis) ) {
             // we're on a One input
             // if we WERE on a Zero input, this is an 'edge' - a 'tap'
             if ( !lastInput ) {
                 lastTap = Time.time;
                 lastInput = true;
                 tapCount++;
                 if ( tapCount == 1 ) { notify.SendMessage("Tap" + axis); }
                 if ( tapCount == 2 ) { notify.SendMessage("DoubleTap" + axis); }
                 // etc.
                 if ( tapCount == maxtaps ) tapCount = 0;
             }
         } else {
             lastInput = false; // we're on a Zero input
             if ( Time.time - lastTap > leeway ) tapCount = 0; // clear taps if it's been too long
         }
         yield;
     }
 }
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 user5200 · Nov 01, 2012 at 03:46 AM 0
Share

Thanks,

Sorry if I was a bit sharp with you the other time, I was just having a bad day. I will try this out and if it works, I will accept your answer.

avatar image Loius · Nov 02, 2012 at 12:46 AM 0
Share

Oh, no, no, you weren't sharp at all. I just deleted the answer to keep this question 'clean' so nobody would have to scroll past a wrong answer to get to a mayberight one. :)

avatar image
0

Answer by lm2malicl · Feb 08, 2018 at 09:52 PM

Try this code it returns a bool so you can set a move bool equal to it. You could use a thershold for the directionalInput.x if you are using a joystick so they dont have to go all the way back to zero.

 public bool isRunning = false;
 bool firstPress = false;
 float pressTime = .25f;
 float currentTime = 0;
 int axisBeforeZero=0;
 int previousAxis = 0;
 
 bool CheckRunning(){
     if(currentTime<=0){//if the player has not provided new input in the time limit reset everything
         axisBeforeZero=0;
         firstPress=false;
     }
     if(directionalInput.x==0){//player is not currently giving movement input...equivalent to releasing the button or axis
         if(previousAxis!=0){//first frame after the axis is released and the player had been moving in a direction
             axisBeforeZero=previousAxis;//keep track of the direction the player had been moving
             previousAxis=0;//set previousAxis to zero
             currentTime=pressTime;//give the player time to complete the double tap
             return(false);
         }else{
             currentTime-=Time.deltaTime;
             return(false);
         }                                                                                                                 
     }else if(directionalInput.x!=0){
         int xDirection=(int)Mathf.Sign(directionalInput.x);//gets the direction the player is currently trying to move
         if(xDirection==previousAxis&&isRunning){//keep running if the player is currently running the same direction
             currentTime-=Time.deltaTime;
             return(true);
         }else if(xDirection==axisBeforeZero&&previousAxis==0){//player has pressed the axis in the same direction within the required time
             previousAxis=xDirection;//set the previous axis for the next frame
             if(firstPress){//the player has already pressed the axis once 
                 currentTime-=Time.deltaTime;
                 return(true);
             }else{//this is the first time the player has pressed the axis
                 firstPress=true;
                 currentTime=pressTime;
                 return(false);  
             }
         }else if(xDirection!= axisBeforeZero){//the player has changed directions or started moving from zero
             previousAxis=xDirection;
             currentTime=0;
             return(false);
         }else{
             currentTime-=Time.deltaTime;
             return(false);
         }
     }else{//return the current running bool to prevent compiling errors... this stament of code should NEVER RUN
         currentTime-=Time.deltaTime;
         return(isRunning);                                                                                                                                                                                                                                                                           
     }
 }


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

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to use Xinputdotnet or other methods of proper Xbox input on mac os x 1 Answer

C# Input.GetAxis Increment/Decrement Value 0 Answers

Achieve framerate independent AddTorque rotaion with mouse drag. 2 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