- Home /
Using the same controller button twice
I'm so close with this I can taste it. I think.
I'm using a camera switch script:
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.camera.active = true;
camera2.camera.active = false;
}
function Update () {
if(Input.GetButtonDown("Inventory")){
camera1.camera.active = false;
camera2.camera.active = true;
}
if(Input.GetKeyDown("l")) {
camera2.camera.active = false;
camera1.camera.active = true;
}
}
What I'd like to do is use the same button to enable and disable it. (essentially I'm using it to switch between my main camera, and a menu) Right now "Inventory" takes you into it, but "l" takes you out. Reason being, if I assign both to "Inventory" then nothing happens. Now, I suspect that all that's needed is a simple "if" statement, such as:
if({camera2.camera.active = true;)
but being so new, I'm not sure if that's
A: Correct, or B: Where I should put it anyway.
Each effort has resulted in errors so far, parse, unexpected token, etc. If someone might be kind enough to help me figure this out, I'd be grateful. God bless.
Answer by RyanZimmerman87 · Sep 13, 2013 at 04:57 AM
public Camera camera1;
public Camera camera2;
bool camera1ActiveBool;
void Start () {
camera1.camera.active = true;
camera2.camera.active = false;
camera1ActiveBool = true;
}
function Update () {
//use whatever button you want to toggle
if(Input.GetKeyDown("l")) {
if (camera1ActiveBool == true)
{
camera1.camera.active = false;
camera2.camera.active = true;
camera1ActiveBool = false;
}
else if (camera1ActiveBool == false)
{
camera1.camera.active = true;
camera2.camera.active = false;
camera1ActiveBool = true;
}
}
}
I am assu$$anonymous$$g all your other syntax is correct all I did was swap the variables to C# since I don't use JavaScript but just set up a bool like I did and it's very easy.
Also did you remember to attach the camera1 and camera2 variables in the Unity Inspector of this script? That is probably the problem with the error you posted. You should have 2 cameras in your scene, you need to drag each one into the correct place within the script in the Unity Inspector.
Apologies for formatting editing this thing can be a pain...
Hey Ryan,
Thanks for putting that together, will definitely check it out. The two cameras were both in the scene, main camera on player, pause menu camera high in the clouds. Here's the output of that camera:
As long as I keep the "enter picture" and "exit picture" (not actual names, just my examples) keys as separate keys, then everything is fine and works great. It's just when I was trying to make them one key that I had the issue.
EDIT: Once again, fantastic work Ryan, thanks! I changed one function to a void and everything ran swim$$anonymous$$gly. =) Quick question: After my initial post I added a pause component, since, you know, getting killed while you're in a menu would suck. Not sure how to implement it in C# though. Here's the code (with pause) in js:
EDIT 2: NV$$anonymous$$, a little Time.timescale action did the trick =)
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.camera.active = true;
camera2.camera.active = false;
}
function Update () {
if(Input.GetButtonDown("Inventory")){
camera1.camera.active = false;
camera2.camera.active = true;
paused = true;
Time.timeScale = 0;
}
if(Input.Get$$anonymous$$eyDown("l")) {
camera2.camera.active = false;
camera1.camera.active = true;
paused = false;
Time.timeScale = 1;
}
}
Glad you got it working! All you need is the Time.timeScale to pause the enemies in the Update and FixedUpdate.
But just one more tip is that you may find that paused bool to eventually be very useful. Depending on how complex your project gets you could set a public static paused variable and use it to disable multiple scripts simultaneously, deter$$anonymous$$e which GUI components to show or not show etc (GUI components can have some nasty side effects even if you can't see the underlying problem behind the new GUI).
So for example if you have a pause menu that fills the whole screen but you still have some buttons on the regular game-play screen, you will need to disable those game-play buttons even if you can't see them currently.
This might help some people new to using GUI's in multiple scripts together, or setting up somewhat complex GUI options menus with many different screens for the first time.
learned that even using a controller there are some weird button quirks. While I can't walk, I can still look around, lol.
If you can't walk it sounds like your timeScale was not reset. Are the enemies not able to move at this time either?
Answer by sooncat · Sep 13, 2013 at 02:04 AM
camera.active = !camera.active;
Thanks for the answer, Soon, but unfortunately no dice, so far. I've no doubt it's user (me) error. The best I've gotten is this error:
$$anonymous$$issingComponentException: There is no 'Camera' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Camera to the game object "Player". Or your script needs to check if the component is attached before using it. CameraSwitch.Update () (at Assets/Standard Assets/Scripts/CameraSwitch.js:30)
Where exactly did you put that piece? That should work
like I said, user error :)
about 3 _ 4 different places,including:
after the two camera variables (line 5ish) after function start (abt line 9) after each getinput statement, both together and separately.
$$anonymous$$d you, I added no brackets, but didn't get any game breaking errors, either.
no worries at all, I appreciate the valuable $$anonymous$$ching tool you gave me. "Teach a man to fish." Am grateful for people like you, Ryan, and others who have so generously helped me either with code, or pointing me in the right direction to continue learning.
Your answer
Follow this Question
Related Questions
Js to c# conversion 1 Answer
Object will not instantiate 1 Answer
Create a if statement using GetComponent? 1 Answer
Error with Camera WorldtoScreenPoint 1 Answer