• 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
Question by Conect11 · Oct 06, 2013 at 12:26 PM · c#buttondisablegamepad

Temporarily disable gamepad button

Good morning (afternoon, evening, fill in the blank)

Am wondering if there is a way to disable a gamepad button in certain contexts. (I did search, only found t$$anonymous$$ngs about GUI Buttons)

For instance: when the player enters into a conversation, I'd like the pause menu button to be disabled. Currently it is mapped to what would be the circle button on a PS3 controller. (don't worry about specifics there, only used PS3 controller as an example, it's actually a Logitech f310)

Thanks for any and all help / advice / points in the right direction. God bless.

Comment

People who like this

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

Answer by vexe · Oct 06, 2013 at 12:32 PM

When you enter a conversation/cutscene/whatEverItIsThatYou'reTryingToDisableInputFor, set a boolean acceptInput to false. When your process your input (your circle button for example) check to make sure that acceptInput is true before you do the processing, otherwise return.

 public bool acceptInput = true;

 void EnterConversation()
 {
   acceptInput = false;
   // code...
 }
 
 void ProcessInput()
 {
    if (!acceptInput) 
        return;
    if (circle button is pressed)
    {
        Pause();
    }
    else if (....)
 }

And of course, don't forget to set it back to true when the conversion is over.

I appreciate your feedback on my answer.

EDIT: I didn't really wanna tidy up your script, instead I put some comments here and there just to let you know what redundancies you have, I thought it's more educational t$$anonymous$$s way. So take the $$anonymous$$nts and t$$anonymous$$ngs I pointed out for you, modify your actual scripts and then if you still have any questions add the modified scripts as an edit to your question. And please work on your formatting skills.

 public class UrusalimShop1DialogueCameraSwitch : MonoBehaviour
 {
     public bool acceptInput = true;
      
     // consider using arrays instead of separate variables, what if at one point you decided to and another camera, and another and another, etc
     // t$$anonymous$$s would get really messy pretty quick
     // i'd use a public Camera[] cams;
     // and then make a property for the first cam (since you're using it a lot)
     // public Camera FirstCam {private set {cams[0] = value;} get {return cams[0];}}
     public Camera camera1;
     public Camera camera2;
     public Camera camera3;
      
     // you don't actually need t$$anonymous$$s variable, because you're setting it to true when camera1.active is true, and false when it's false
     // so it's basically the same t$$anonymous$$ng as camera1.active
     // in other words, in your calcuations, just use if (camera1.active) instead of (camera1ActiveBool)
     bool camera1ActiveBool;
      
     void Start ()
     {
         acceptInput = false;
         //camera1.camera.active = true;  no need to access the camera component from your camera1 object, since it's already a camera :)
         camera1.active = true;
         camera2.active = false;
         camera3.active = false;
         camera1ActiveBool = true;
     }
      
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             // you don't need to say if (x == true)
             // the way conditional statements work is, they take a boolean expression, w$$anonymous$$ch is anyt$$anonymous$$ng that returns either true or false.
             // for example: 1 > 2 (returs false), 10 == 10 (returns true).
             // Now, since the boolean expression itself returns a true/false, I don't need to say if ((10 == 10) == true), that's like saying if (true == true), w$$anonymous$$ch turns to if (true).
             // in other words, since camera1ActiveBool is a boolean (somet$$anonymous$$ng that returns true/false) you don't need to say:
             // if (camera1ActiveBool == true), if you wanted to check if it was false, you just negate it with the pound sign:
             // if (!camera1ActiveBool)

             if (camera1ActiveBool)
             {
                 // setting component.active = boolean; will either activiate/deactiviate the gameObject that has t$$anonymous$$s component
                 // setting component.enabled = boolean; (like a script for example) will only enable/disable that component, and won't affect the gameObject its attached to
                 // in other words, if you have a script, attached to a gameObject and you wanted to disable the whole gameobject (w$$anonymous$$ch will in turn disable the functionality of the script as well), use myScript.active = false; or myScript.gameObject.active = false; (same t$$anonymous$$ng)
                 // but if you wanted to ONLY disable that script, you'd do myScript.enabled = false;
                 // So, if the gameObject that has your camera component doesn't have any other componet beside the camera, you could just do
                 // cameraN.gameObject.SetActive(boolean) (don't use camera1.active, it's obselete, deprecated)
                 // otherwise cameraN.enabled = boolean;
                 // in other words, you don't need to use both enabled and active at the same time.

                 camera1.active = false;
                 camera2.active = true;
                 camera2.enabled = true;
                 camera1.enabled = false;
                 camera3.enabled = false;
                 camera1ActiveBool = false;
                  
                 Time.timeScale = 0;
             }
              
             // no need for the else-if here, a boolean could either be true or false, you just need a simple if-else statement here. 
             // you'd use an else-if for somet$$anonymous$$ng, that could have more than 2
             // values, like if (player.state == state.walking) { } else if (player.state == state.idle) { } else if (player.state == state.attacking) etc
             // else if (camera1ActiveBool == false)
             else
             {
                 camera1.active = true;
                 camera2.active = false;
                 camera1.enabled = true;
                 camera2.enabled = false;
                 camera3.enabled = true;
                  
                 camera1ActiveBool = true;
                 Time.timeScale = 1;
             }
         }
     }

 Pause Menu Switch

     using UnityEngine;
     using System.Collections;
      
     public class Menu : MonoBehaviour
     {
      
         public Camera camera1;
         public Camera camera2;
         bool camera1ActiveBool;
          
         void Start ()
         {
             camera1.active = true;
             camera2.active = false;
             camera1ActiveBool = true;
         }
          
          
         void Update ()
         {
             //use whatever button you want to toggle
             if(Input.GetButtonDown("Inventory"))
             {
              
                 // t$$anonymous$$s whole logic is redundant, notice how it's repitive, and the values are kinda the opposite?
                 //if (camera1ActiveBool)
                 //{
                     //camera1.active = false;
                     //camera2.active = true;
                     //camera1ActiveBool = false;
                     //Time.timeScale = 0;
                 //}
                 //else
                 //{
                     //camera1.active = true;
                     //camera2.active = false;
                     //camera1ActiveBool = true;
                     //Time.timeScale = 1;
                 //}

                 // all you need to do is:
                 camera1.active = !camera1ActiveBool;    // t$$anonymous$$s means, if cam1ActBool was true, then !cam1ActBool is false, thus camera1.active = false;
                 camera2.active = camera1ActiveBool;
                 camera1ActiveBool = !camera1ActiveBool; // toggle it, change it to the opposite state
                 Time.timeScale = camera1ActiveBool ? 0 : 1; // t$$anonymous$$s is called the ternary operator, its equivalent would be if (camActiveBool) timescale = 0; else timeScale = 1;

                 // use the same type of reduction on the previous if-else statement, (in the previous script)
                 // didn't wanna do it for you cause I thought it'd be nice if you practise it yourself
             }
         }
      }
Comment
clunk47
TechSupportIncoming1

People who like this

2 Show 5 · 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 AndyMartin458 · Oct 06, 2013 at 05:33 PM 0
Share

This will work, but doesn't allow keyboard logic to be split up very easily. When you have a Unity GUI input label active, it seems like Unity auto-ignores other keys like WASD. Any way to simulate that elsewhere?

avatar image Conect11 · Oct 06, 2013 at 06:05 PM 0
Share

Vexe, thanks for your answer. Just got home from working and am looking forward to taking it for a test drive. Quick question, if you wouldn't mind educating a newbie: Everything up until line 9ish makes sense, setting a public bool, setting acceptinput to false. What is the purpose for the lines underneath? Honest, legitimate question, just trying to learn. Here's the code I'm trying to put this on, which essentially switches the camera to my "conversation view." For posterity I'll also include my pause camera script as well, though I'll assume that only the first one would need to be modified.

Conversation Camera Switch using UnityEngine; using System.Collections;

 public class UrusalimShop1DialogueCameraSwitch : MonoBehaviour {
 public bool acceptInput = true;
 
          public Camera camera1;
      
     public Camera camera2;
     
     public Camera camera3;
      
     bool camera1ActiveBool;
      
      
     void Start () {
     acceptInput = false; 
     camera1.camera.active = true;
      
     camera2.camera.active = false;
         
     camera3.camera.active = false;    
      
     camera1ActiveBool = true;
         
     }
      
      
     void  OnTriggerEnter ( Collider other  ){
 if (other.gameObject.tag == "Player") {
 }
      
     if (camera1ActiveBool == true)
     {
     camera1.camera.active = false;
     camera2.camera.active = true;
     camera2.enabled = true;
     camera1.enabled = false;
     camera3.enabled = false;        
     camera1ActiveBool = false;
                 
      Time.timeScale = 0;
     }
      
     else if (camera1ActiveBool == false)
     {
     camera1.camera.active = true;
     camera2.camera.active = false;
     camera1.enabled = true;
     camera2.enabled = false;
     camera3.enabled = true;        
      
     camera1ActiveBool = true;
                  Time.timeScale = 1;
     }
      
     }
     }







Pause Menu Switch

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
 
 
          public Camera camera1;
      
     public Camera camera2;
      
     bool camera1ActiveBool;
      
      
     void Start () {
      
     camera1.camera.active = true;
      
     camera2.camera.active = false;
      
     camera1ActiveBool = true;
         
     }
      
      
     void Update () {
      
     //use whatever button you want to toggle
     if(Input.GetButtonDown("Inventory")){
      
     if (camera1ActiveBool == true)
     {
     camera1.camera.active = false;
     camera2.camera.active = true;
      
     camera1ActiveBool = false;
                 
      Time.timeScale = 0;
     }
      
     else if (camera1ActiveBool == false)
     {
     camera1.camera.active = true;
     camera2.camera.active = false;
      
     camera1ActiveBool = true;
                  Time.timeScale = 1;
     }
      
     }
     }
     }
avatar image vexe · Oct 07, 2013 at 08:44 AM 0
Share

well first let's clear out all redundant code you have, and then you can ask, cuz honestly I don't know what you're asking me about :D - Answer updated. Please upvote if you found my tips helpful. If you want anything else, let me know.

avatar image vexe · Oct 10, 2013 at 08:18 AM 0
Share

@Conect11 how did it go? Do you still in need of anything? Tell me :) If not, please tick my answer as correct.

avatar image Conect11 · Oct 11, 2013 at 06:09 PM 0
Share

hey Vexe,

sorry about not ticking that. I ended up having to do an end around that bypassed the issue, so hadn't been back to it.

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

17 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

Related Questions

Distribute terrain in zones 3 Answers

Change button on focus 0 Answers

Make button disappear, but show label. 1 Answer

Multiple Cars not working 1 Answer

JS to C# Translation? 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