Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MarekLg · Jul 31, 2018 at 02:13 PM · inputbuttonaxisjoystick

D-Pad as Button instead of Axis

This is just a sort of an update question as there already have been a couple of the like, but all some years old.

So: is there a "intended" way of getting the joystick D-Pad as buttons instead of axis (e.g. for using GetButtonDown()). I just can't get my head around this not already being a thing.

Cheers

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Legend_Bacon · Jul 31, 2018 at 03:02 PM

Hello there,

As far as I can remember, using the Dpad or Joystick exactly as you would use a button won't work.

Simply because instead of having two states (pressed or not), an axis can have thousands of values between -1 and 1. (Or 0 and 1)

What you can do is say:

• If the horizontal axis is at less than -0.75f, then it's a left Dpad press.

• If the horizontal axis is at more than 0.75f, then it's a right Dpad press.

• Else, between -0.75f and 0.75f the buttons are not being pressed.

Pair that with a few bools, and you can have a system that's equivalent to Input.GetKey, GetKeyDown and GetKeyUp.

You can find a little more on this thread.

Also, it's up to you to define and test how high the sensitivity value (that -0.75f and 0.75f) should be, i.e. how hard your players need to press the Dpad for a button press to trigger.


I hope that helps!

Cheers,

~LegendBacon

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 xdarned · Dec 30, 2020 at 12:46 AM

Hi! I came up with something. You are going to create a game object and call it dPadTranslator. In it you are going to add a new script, call this DPadTranslator. Once you get this open up the script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class DPadTranslator : MonoBehaviour
 {
     public int up_buttonDown;
     [SerializeField] private bool up_chkDown;
 
     private void Update() {
         if(Input.GetAxis("vertical") > 0 && !up_chkDown) {
             up_chkDown = true;
             StartCoroutine(Up_ButtonDownCo());
         } else if(Input.GetAxis("vertical") == 0) {
             up_chkDown = false;
         }
     }
 
     IEnumerator Up_ButtonDownCo() {
         up_buttonDown = 1;
         yield return new WaitForEndOfFrame();
         up_buttonDown = 0;
     }
 
 }
 

Now pay attention: the idea is to emulate the ButtonDown, so when you push the dPad down it will send a 1 for the rest of the frame and then it will send a 0. Until you release the button. This will act like Input.GetButtonDown. Now, in the object you want to behave like you have pressed the button instead of an axis, you will read the up_buttonDown variable, that's why it's public. In other words, you will not read directly the input but the translator of the input wich will give you a short signal for the end of the frame.

This script works for the up button so you have to configure it in the proyect settings as "vertical" and asign it to the 8-axis. So: - go to Edit->project settings->InputManager and create a new input. Set the name to "vertical", Type JoyStick Axis, Axis 8-Axis.

Once you test this work rewrite the code so it can handle the other buttons. You just have to repeat the if block in the update three more times, once for each button and the same for the coroutines. Change the names of the variables to left, down, etc, as well as the coroutine.

This should do the trick!

If anything, e-mail me: xdarned@gmail.com-

Comment
Add comment · Show 1 · 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 xdarned · Dec 30, 2020 at 12:48 AM 0
Share

Don't forget to set up the "horizontal" axis in th project preferences!

avatar image
0

Answer by Ebonicus · Jan 28 at 01:08 AM

@MarekLg @vdarned I do this often:

Just set a bool and disable it once it's used:

 bool dpadVerticalBlocked = false;
 
 //If they hit full dpad and its allowed, do stuff
 if ( (Input.GetAxis("DPadVertical") ==1) && (dpadVerticalBlocked==false) ) {
 
       //Do your button down stuff here
       dpadVerticalBlocked=true; //Disable it
 
 } else {
     //if they release, allow them to hit it again.
    if ( Input.GetAxis("DPadVertical") ==0) { dpadVerticalBlocked=false;}
 }

Comment
Add comment · Show 1 · 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 xdarned · Jan 30 at 05:09 PM 0
Share

There is no case in using an if inside the else, though... You are double checking the same thing twice. What other case could be if not one? Also you can get rid of the parentesis in the first statement.

In the other hand, if you don't wait until the end of the frame you could get other scripts not reading on time the change of the bool on the current frame.

Use the coroutine to grant that the bool will be visible for everyone while the current frame is running.

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

122 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

Related Questions

Steelseries 3gc controller key mappings 0 Answers

How do you change the joystick axis boundary from a circle to a square? 1 Answer

Using Right Joystick to Select Weapon in Weapon Wheel 1 Answer

A button press is not working unless I also press an axis at the same time. 1 Answer

Controller Joystick Hold Delay Logic? 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