- Home /
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 things 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.
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 this way. So take the hints and things 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
// this 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 this 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 thing 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, which is anything 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), which turns to if (true).
// in other words, since camera1ActiveBool is a boolean (something 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 this 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 (which will in turn disable the functionality of the script as well), use myScript.active = false; or myScript.gameObject.active = false; (same thing)
// 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 something, 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"))
{
// this 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; // this 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; // this 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
}
}
}
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?
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 $$anonymous$$d 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 : $$anonymous$$onoBehaviour {
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 $$anonymous$$enu Switch
using UnityEngine;
using System.Collections;
public class $$anonymous$$enu : $$anonymous$$onoBehaviour {
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;
}
}
}
}
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.
@Conect11 how did it go? Do you still in need of anything? Tell me :) If not, please tick my answer as correct.
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
Follow this Question
Related Questions
Make button disappear, but show label. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Saving settings info in between scenes 1 Answer
Hidden Canvas, doesn't disable interaction with buttons. 1 Answer