Search Unity

Adding snippet of code that will unbind a duplicate keystroke in "controls" menu?

Discussion in 'Scripting' started by Abuscaglio, Mar 22, 2018.

  1. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    Hey guys,

    So I wrote this snippet of code for my key assignment section of my game's settings menu. currently, if I click on a specified button, and then input a keyboard key, they button text changes to the pressed key. I would like an included function that states if (for example) the "A" key is already assigned to action1 and pressed to assign to action2, it will remap to action2 and action1's keybind will be cleared and replaced with "-". Im fairly green at coding so I'm wondering what the snippet of code would be and where it would be placed in the following script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class KeyBindScript : MonoBehaviour {
    7.  
    8.     private Dictionary<string, KeyCode> Keys = new Dictionary<string, KeyCode> ();
    9.  
    10.     private GameObject currentKey;
    11.  
    12.     public Text forward, backwards, movleft, movright, leanleft, leanright, run, jump, crouch, interact, attack, clairvoyance, journal, inventory, tasks, watch, quickwheel, pausemenu;  
    13.  
    14.     void Start ()
    15.     {
    16.         Keys.Add("Forward", KeyCode.W);
    17.         Keys.Add("Backwards", KeyCode.S);
    18.         Keys.Add("MovLeft", KeyCode.A);
    19.         Keys.Add("MovRight", KeyCode.D);
    20.         Keys.Add("LeanLeft", KeyCode.Q);
    21.         Keys.Add("LeanRight", KeyCode.E);
    22.         Keys.Add("Run", KeyCode.LeftShift);
    23.         Keys.Add("Jump", KeyCode.Space);
    24.         Keys.Add("Crouch", KeyCode.LeftControl);
    25.         Keys.Add("Interact", KeyCode.F);
    26.         Keys.Add("Attack", KeyCode.Mouse0);
    27.         Keys.Add("Clairvoyance", KeyCode.Mouse1);
    28.         Keys.Add("Journal", KeyCode.J);
    29.         Keys.Add("Inventory", KeyCode.I);
    30.         Keys.Add("Tasks", KeyCode.X);
    31.         Keys.Add("Watch", KeyCode.C);
    32.         Keys.Add("QuickWheel", KeyCode.Mouse3);
    33.         Keys.Add("PauseMenu", KeyCode.Escape);
    34.  
    35.         forward.text = Keys ["Forward"].ToString ();
    36.         backwards.text = Keys ["Backwards"].ToString ();
    37.         movleft.text = Keys ["MovLeft"].ToString ();
    38.         movright.text = Keys ["MovRight"].ToString ();
    39.         leanleft.text = Keys ["LeanLeft"].ToString ();
    40.         leanright.text = Keys ["LeanRight"].ToString ();
    41.         run.text = Keys ["Run"].ToString ();
    42.         jump.text = Keys ["Jump"].ToString ();
    43.         crouch.text = Keys ["Crouch"].ToString ();
    44.         interact.text = Keys ["Interact"].ToString ();
    45.         attack.text = Keys ["Attack"].ToString ();
    46.         clairvoyance.text = Keys ["Clairvoyance"].ToString ();
    47.         journal.text = Keys ["Journal"].ToString ();
    48.         inventory.text = Keys ["Inventory"].ToString ();
    49.         tasks.text = Keys ["Tasks"].ToString ();
    50.         watch.text = Keys ["Watch"].ToString ();
    51.         quickwheel.text = Keys ["QuickWheel"].ToString ();
    52.         pausemenu.text = Keys ["PauseMenu"].ToString ();
    53.  
    54.  
    55.     }
    56.  
    57.     void Update ()
    58.     {
    59.         if (Input.GetKeyDown(Keys["Forward"]))
    60.         {
    61.             //Move Forward
    62.         }
    63.         if (Input.GetKeyDown(Keys["Backwards"]))
    64.         {
    65.             //Move Backwards
    66.         }
    67.         if (Input.GetKeyDown(Keys["MovLeft"]))
    68.         {
    69.             //Move Left
    70.         }
    71.         if (Input.GetKeyDown(Keys["MovRight"]))
    72.         {
    73.             //Move Right
    74.         }
    75.         if (Input.GetKeyDown(Keys["LeanLeft"]))
    76.         {
    77.             //Lean Left
    78.         }
    79.         if (Input.GetKeyDown(Keys["LeanRight"]))
    80.         {
    81.             //Lean Right
    82.         }
    83.         if (Input.GetKeyDown(Keys["Run"]))
    84.         {
    85.             //Run
    86.         }
    87.         if (Input.GetKeyDown(Keys["Jump"]))
    88.         {
    89.             //Jump
    90.         }
    91.         if (Input.GetKeyDown(Keys["Crouch"]))
    92.         {
    93.             //Crouch
    94.         }
    95.         if (Input.GetKeyDown(Keys["Interact"]))
    96.         {
    97.             //Interact With Object
    98.         }
    99.         if (Input.GetKeyDown(Keys["Attack"]))
    100.         {
    101.             //Attack
    102.         }
    103.         if (Input.GetKeyDown(Keys["Clairvoyance"]))
    104.         {
    105.             //Initialize Clairvoyance
    106.         }
    107.         if (Input.GetKeyDown(Keys["Journal"]))
    108.         {
    109.             //Open Journal
    110.         }
    111.         if (Input.GetKeyDown(Keys["Inventory"]))
    112.         {
    113.             //Open Inventory
    114.         }
    115.         if (Input.GetKeyDown(Keys["Tasks"]))
    116.         {
    117.             //Open Task Menu
    118.         }
    119.         if (Input.GetKeyDown(Keys["Watch"]))
    120.         {
    121.             //Look At Watch
    122.         }
    123.         if (Input.GetKeyDown(Keys["QuickWheel"]))
    124.         {
    125.             //Open Quick Wheel
    126.         }
    127.         if (Input.GetKeyDown(Keys["PauseMenu"]))
    128.         {
    129.             //Open Pause Menu
    130.         }
    131.     }
    132.  
    133.     void OnGUI()
    134.     {
    135.         if (currentKey != null) {
    136.             Event e = Event.current;
    137.             if (e.isKey) {
    138.                 Keys [currentKey.name] = e.keyCode;
    139.                 currentKey.transform.GetChild(0).GetComponent<Text> ().text = e.keyCode.ToString ();
    140.                 currentKey = null;
    141.             }
    142.         }
    143.     }
    144.  
    145.     public void ChangeKey(GameObject clicked)
    146.     {
    147.         currentKey = clicked;
    148.     }
    149. }