- Home /
Way to swap left and right mouse buttons?
Is there a simple, easy way to get Unity to recognize the left mouse button as the right mouse button, and vice versa, without doing a bunch of modifications to my code?
whenever you use Get$$anonymous$$ouseButton/Up/Down, the parameter is the mouse button. 0 is left and 1 is right (I think). just use them vice versa
This is the sort of thing I'm trying to avoid. There are other cases (such as clicking buttons) that would require different solutions. I want to offer my users the option to swap mouse buttons if they'd prefer because someone requested it, but it would be more effort than it's worth if there's not a solution that wouldn't require me to go through my code and change a bunch of stuff.
How are you handling input? There is a very easy way if you are using the Input$$anonymous$$anager that Unity provides. Located under Edit >> Project Settings >> Input
Answer by SvendErik · Feb 08, 2017 at 04:06 PM
If you use the premade input, Input.GetButton("Fire1") for example, then just change it either everywhere in the code (shift the 1 with 2 after Fire, for example). Or you can do it in the editor, so that every script will have them swapped. You can do it via edit - project settings - input. I do not recommend that, however. It can be very confusing.
But if you used Input.GetKey instead, then you pretty much just have to change it manually everywhere. You could maybe even make it a setting in your game. You could have a UI button, which calls a method, that changes all your Keycode variables to what you want. You can make a keycode variable like this: public KeyCode myKey = KeyCode.LeftArrow; You can then use it like this
Update() { if (Input.GetKey(myKey)) { MoveLeft(); } }
And change it like this:
myKey = KeyCode.RightArrow;
I've most likely written a lot you already know, but it might help some others to. Feel free to tag me with a question, if you want me to explain something of this a little better.
Answer by SvendErik · Feb 08, 2017 at 08:59 AM
I suppose you're using Input.GetButtonDown("Fire1")) and Fire2. You can make this into variables.
Public string leftMouse = "Fire1" Public string rightMouse = "Fire2" Book swapped = false;
You can then use the variable when checking input
if (Input.GetButtonDown(leftMouse)) { // Do whatever you want with the left click }
And the same just with rightMouse You can now make a UI button, which calls this method
Public void SwapButtons () { if (swapped == false) { leftMouse = "Fire2" rightMouse = "Fire1" } else { leftMouse = "Fire1" rightMouse = "Fire2" } }