• 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 pauld1988 · Dec 14, 2016 at 11:10 PM · controllerinputs

how to get GetAxisRaw to work with other buttons

Hi everyone,

im wondering how to make an axis work like a button im using a guitar hero controller and have set the 6 main button as a,b,c,d,e,f, and they work fine i set up the yaxis as a button and that works but they dont work together

"strum up" returns true on its own but if im holding another button down it stays false

how ever if i hold strum up true and press A or B then it does play the sounds
its like its backwards

many thanks in advance

using UnityEngine; using System.Collections;

[RequireComponent(typeof(AudioSource))]

public class GuitarSetup : MonoBehaviour {

 public AudioClip Cdown;
 public AudioClip Cup;
 public AudioClip Emdown;
 public AudioClip Emup;
 public AudioClip Gdown;
 public AudioClip Gup;
 public AudioClip Ddown;
 public AudioClip Dup;
 public AudioSource source;

 public bool a;
 public bool b;
 public bool c;
 public bool d;
 public bool e;
 public bool f;
 public bool strumup = false;
 void Awake (){
     source = GetComponent<AudioSource>();


 

 }





 // Use t$$anonymous$$s for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {



     //if(Input.GetButtonDown("strum down")){
     if( Input.GetAxisRaw("strum up") != 0)
     {
         if(strumup == false)
         {
             
             //noteonedown();
             strumup = true;
             noteonedown();
         }
     }
     if( Input.GetAxisRaw("strum up") == 0)
     {
         strumup = false;
     }    

         //noteonedown();

     
     if(Input.GetButtonDown("joy 0")){
         

         a = true;

     }
     if(Input.GetButtonDown("joy 1")){

         b = true;

     }
     if(Input.GetButtonDown("joy 2")){

         c = true;

     }
     if(Input.GetButtonDown("joy 3")){

         d = true;

     }
     if(Input.GetButtonDown("joy 4")){

         e = true;

     }
     if(Input.GetButtonDown("joy 5")){

         f = true;

     }


     if(Input.GetButtonUp("joy 0")){


         a = false;

 }
     if(Input.GetButtonUp("joy 1")){

         b = false;

 }
 if(Input.GetButtonUp("joy 2")){

     c = false;

 }
 if(Input.GetButtonUp("joy 3")){

     d = false;

 }
 if(Input.GetButtonUp("joy 4")){

     e = false;

 }
 if(Input.GetButtonUp("joy 5")){

     f = false;

 }

} void noteonedown(){ if(a == true) { CChord();

     }
     else 
     {
         notetwodown();
     }
 }
     void notetwodown(){
         if(b == true)
         {

         EmChord();
         }
         else 
         {
         notethreedown();
         }
 }
 void notethreedown(){
     if(c == true)
     {

         GChorddown();
     }
     else 
     {
         notefourdown();
     }
 }
     void notefourdown(){
     if(d == true)
         {

         DChord();
         }
         else 
         {
         notefivedown();
         }
     }
     void notefivedown(){
     if(e == true)
         {

         GChordUp();
         }
         else 
         {
         notesixdown();
         }
     }
     void notesixdown(){
     if(f == true)
         {

             source.clip = Cup;
             source.Play();
         }
         else 
         {
         print("open strum");
         }
 }
 public void CChord () {
     source.PlayOneShot(Cdown);
 }
 public void CChordUp () {
     source.PlayOneShot(Cup);
 }
 public void EmChord () {
     source.PlayOneShot(Emdown);
 }
 public void EmChordup () {
     source.PlayOneShot(Emup);
 }
 public void GChorddown () {
     source.PlayOneShot(Gdown);
 }
 public void GChordUp () {
     source.PlayOneShot(Gup);
 }
 public void DChord () {
     source.PlayOneShot(Ddown);
 }
 public void DChordup () {
     source.PlayOneShot(Dup);
 }

}

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
Best Answer

Answer by Max_Bol · Dec 15, 2016 at 12:56 AM

First, I would say that using GetAxisRaw comes with a price that you should watch out for (if you haven't already). Make sure the "Dead" float for each "float" based button/triggers is relatively close to 0.3. The t$$anonymous$$ng is that guitar axis-based strum can get loose over time and the error margin can reach even as $$anonymous$$gh as 0.3 (meaning pressing the strum less than 1/3 either up or down doesn't return anyt$$anonymous$$ng).

For t$$anonymous$$s, I suggest you use GetAxis instead of GetAxisRaw and set a Dead zone manually with a float that can be set by the user in the options menu of your project. (Kinda like how steering wheel offers an internal option of adjusting the "center" of the wheel.) In terms of internal mecanism, GetAxisRaw does the exact same t$$anonymous$$ng as GetAxis with a manual dead zone. (It does a if() statement where whenever GetAxis float is $$anonymous$$gher then Dead, it return 1, else return 0)

Personally, I would also avoid using so many if() statement for each notes. I know it's easy, but you can do somet$$anonymous$$ng more simple and efficient.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GuitarSetup : MonoBehaviour
 {
 
     public AudioSource source;
     public AudioClip[] NotesSounds;
     public bool StrumIsActive = false;
     public float DeadZoneStrum = 0.3f;
 
 
 
     void Awake ()
     {
         source = GetComponent<AudioSource> ();
     }
     
     // Update is called once per frame
     void LateUpdate ()
     {
         switch (StrumIsActive) {
         default:
             if (Input.GetAxis ("Stum up") <= DeadZoneStrum && Input.GetAxis ("Stum up") >= -DeadZoneStrum && StrumIsActive) {
                 StrumIsActive = false;
             }
             break;
         case false:
             if (Input.GetAxis ("Stum up") >= DeadZoneStrum || Input.GetAxis ("Stum up") <= -DeadZoneStrum) {
                 ManageKeyStroke(
                     Input.GetButton ("joy 0"),
                     Input.GetButton ("joy 1"),
                     Input.GetButton ("joy 2"),
                     Input.GetButton ("joy 3"),
                     Input.GetButton ("joy 4"),
                     Input.GetButton ("joy 5"));
             }
             StrumIsActive = true;
             break;
         }
     }
 
     void ManageKeyStroke (bool Key0, bool Key1, bool Key2, bool Key3, bool Key4, bool Key5)
     {
         int NoteToBePlayed = -1;
 
         if(Key5){
             NoteToBePlayed = 5;
         }
         if(Key4){
             NoteToBePlayed = 4;
         }
         if(Key3){
             NoteToBePlayed = 3;
         }
         if(Key2){
             NoteToBePlayed = 2;
         }
         if(Key1){
             NoteToBePlayed = 1;
         }
         if(Key0){
             NoteToBePlayed = 0;
         }
 
         if(NoteToBePlayed >= 0){
             //If only the stum is pressed, NoteToBePlayed will return its initial -1 value and will get ignored. It's what you called "Open strum".
             PlayNote(NoteToBePlayed);
         }
 
         // If You got another script that manage if the right keys were pressed (based on the song), you could send a call from t$$anonymous$$s function toward that script by implementing a "checker" function into that remotely accessible script.
     }
 
     void PlayNote(int Note){
         if(NotesSounds[Note]){
             source.PlayOneShot(NotesSounds[Note]);
         }
         else{
             Debug.LogWarning("An unknown note is being requested : #" + Note);
             //T$$anonymous$$s will get called if the function return a sount that is not part of the NotesSounds array of soundclip.
         }
     }
 }

T$$anonymous$$s does more or less the same t$$anonymous$$ng as you wrote, but in a more direct approach. The switch() statement is really useful when you know you don't need the "else" to be checked every time. In terms of memory, if() and else() gets read by the engine even if they aren't used, unlike switch() w$$anonymous$$ch is simply ignored (non-existant) in the engine memory unless its value returns true. (W$$anonymous$$ch is why switch can't uses ambiguous values and t$$anonymous$$ngs like floats should be avoided with it.

I made use of LateUpdate() instead of Update() for the input. Usually, it's suggested that Update() is used for inputs, but when you want somet$$anonymous$$ng ultra precise that is based on both the UI display and user input, it's better to us LateUpdate(). T$$anonymous$$nk of it as t$$anonymous$$s : During the Update(), your UI is updated and the key color or whatever you're displaying is moving in THAT frame. So if the game perfectly press the button at the frame when the color dot (or whatever) is a the perfect position, $$anonymous$$s input will only be called onto the next frame (so 1 frame late). It might sound ridiculous, but it's noticeable for skilled players. By putting the input into the LateUpdate(), you push the "scan" for the input after the updated display of the UI, but before the next update of the UI. (between both) In a game where the player "avoid" being $$anonymous$$t, Update give 1 frame "preview" of margin to the player. In a game where you want the player to press exactly at the right time when somet$$anonymous$$ng is going on in the UI, you use LateUpdate(). If you ever played a game where you got t$$anonymous$$s mini-game that requires you to press a button when 2 circles (one circle that is growing or shrinking and 1 "static" circle) touch each other, many devs does the mistake of checking it in the Update() w$$anonymous$$ch make it feels like you had to press "ahead" of the actual moment. T$$anonymous$$s is why LateUpdate() is used in t$$anonymous$$s particular setting.

You'll also notice I make use of an array instead of enumerating each SoundClip. T$$anonymous$$s way, you can add more "notes" without altering too much your script. T$$anonymous$$s also allow you to mix keys together for a different sound. (So, if Key 0 and Key 3 is both pressed, you get a different note than if you used Key 0 or Key 3.) But that requires you to manage the NoteToBePlayed integer accordingly. I made the script.

Now, depending on what kind of sound you uses for the Notes, t$$anonymous$$s is not a complete script. It's more of a basic sketchup of a script based on what you wrote that should works. From your script, I guess you priories the early key/button ("joy 0" over "joy 1" over "joy 2", etc.) over the other keys for what sound it plays.

If you want to have dual keys, you'll have to put more soundclips to fills those dual keys sounds and that's where t$$anonymous$$s script above s$$anonymous$$ne the most and it all depends on how good you are a translating 5 bools inputs into a single int to get the right sound out of a clip array. (And also on how many notes sounds you have at your disposition.)

Comment
Add comment · Show 4 · 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 pauld1988 · Dec 15, 2016 at 03:31 AM 0
Share

Hi, thank you for the reply...

this script has the same result as mine how ever StrumIsActive Bool in the inspector is showing true then false then true when its running
with no sound

avatar image Max_Bol pauld1988 · Dec 15, 2016 at 04:08 AM 0
Share

The first thing I would do, in this case, is test out how you Strum trigger returns values.

Something as simple as:

Debug.Log(Input.GetAxis ("Stum up")); And play with it with no other debug calls.

Check what values comes out of it then adjust the DeadZoneStrum based how the results. If you notice fluctuation where pressing the Strum return inconsistent values (like 1-1-1-0.8-0.3-0.4-1-1) as you keep pressing the strum, you might have to uses a different approach.

I tested out my script and it works 100% on my end (though there are many things I would add to it to make it more complete, but that's not the thing here.) I haven't tested it out with a Guitar Hero's guitar because I don't have one right now, but I tested it out with a 360 controller. (It was strange to "use", but it simulated the buttons and triggers).

The problem about the sound might come from something else that affects the AudioSource.

avatar image pauld1988 Max_Bol · Dec 15, 2016 at 04:57 AM 0
Share

okay ive done some digging and checked to see whats happening

using void Update () { if( Input.GetAxis("Strum up") > 0) { Debug.Log(Input.GetAxis ("Strum up")); }
if( Input.GetAxis("Strum up") == 0) { Debug.Log(Input.GetAxis ("Strum up")); }
as it turns out the problem is the controller if i hold the button up it returns 1 witch is right however if i press any button on the controller it sets to 0 and wont detect the key press

avatar image pauld1988 pauld1988 · Dec 15, 2016 at 06:27 AM 0
Share

accepted as working its the guitar hero controller at fault works with the PoV on the controller

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

Using DPad/Joystick in "new" InputSystem as digital rather than analog 1 Answer

How to detect inputs from a dance dance revolution carpet 1 Answer

How can I find out the number of the controller ( new input system) 0 Answers

Setting up inputs for multiple gamepads/controllers 1 Answer

MMD How to export model and animations to Unity as 3rd person controller? 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