Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Its My Idea Of Making A Switch KeyBinding In Settings of Game. Hope Someone Can Find This Help Full.

JIMMY_VASHI04JIMMY_VASHI04 Member
edited June 2020 in Programming

So i've been working on my third game and every time i suffered a situation that what if players didn't like the movement controls or attack controls. so i googled how to make a menu that allows players to set keys what they like to interact. i've seen so many conversations on unity answers but didn't get a single word.

so i've made something by my self hope someone finds it useful.

i will give all steps that i made and followed.

1)make a empty GameObject in the canvas(It Must Remain Active).

2)make a new c# script(I named it "InputManager") here is the full script.

i've tried to explain everything by comments but if you still have a confusion you can massage me.

3)make a button that will active the change input system and will show the KeyBinding.

i've made it like this

3)Now time to modify the movement script//you can as many as you want based on how many interactions can your player do while playing

  private KeyCode forward;
  private KeyCode backward;
  private KeyCode left;
  private KeyCode right;

void Start(){
    if (transform.tag == "Player") {//i use the same script for all player so i check and assign the controls based on the tag

      forward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p1forward","W"));//get the player pfref string and also give a default key
      backward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p1backward","S"));
      left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p1left","A"));
      right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p1right","D"));

    }

    if (transform.tag == "Player2") {    
      forward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p2forward","UpArrow"));
      backward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p2backward","DownArrow"));
      left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p2left","LeftArrow"));
      right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("p2right","RightArrow"));

    }
}

4)make a new script for the button's child text object 

 public string playerPrefName; //write here the playerprefs name 
  // so in my forward button i wrote "p1forward"
  public string defaultKey; //write here the default keybinding
  //for my forward button the default keybinding is "W" so i wrote "W"
  private Text t;

  void Start(){
    t = GetComponent<Text>();
    t.text = PlayerPrefs.GetString(playerPrefName,defaultKey);
  }

this will make sure that every time the game is restarted the button will show currently set keybinding.

also set the buttons navigation to none otherwise you will not be able to assign space key to anything.and all the buttons will remain focused after use.

now you can make more button like this .

thats all you need.It worked perfectly for me.

If you find it useful you can use this and ENJOY.

also thanks for seeing my work....!!!😁

Sign In or Register to comment.