<Standard Disclaimer: This is some Unity code. There's multiple ways to do anything in Unity. My way is not necessarily the best way, it just works.>
The "E" key is a standard action key in most First Person games. The game jam version of Plaguebearer hard coded the "E" key for all actions. I had made a note to set this to a variable which can be set by the player and also got some playtest feedback that people wanted to set their own action key. So, I went looking for the solution to this and here is what I came up with...
The Approach:
1. In the controls setting menu, have a UI button to change the action key
2. On pressing the button, show a panel with instructions to "Press Any Key"
3. Don't do anything until a key is pressed
4. Read keypress
5. Write keypress to playerprefs
6. Read keycode from playerprefs as a variable and set in player script
Here is the code for the settings menu. This sets the key pressed into player prefs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controlsSettings : MonoBehaviour {
public Text actionKeyText;
public string currentActionKey;
public GameObject askKeyPanel;
public bool askKeyUp;
public KeyCode newActionKey;
// Use this for initialization
void Start () {
askKeyUp = false;
currentActionKey = PlayerPrefs.GetString("ActionKey","E");
actionKeyText = actionKeyText.GetComponent<Text>();
actionKeyText.text = "Action Key:" + currentActionKey;
askKeyPanel.gameObject.SetActive(false);
}
public void changeActionKey()
{
askKeyPanel.gameObject.SetActive(true);
askKeyUp = true;
}
void OnGUI()
{
if (askKeyUp)
{
Event e = Event.current;
if (e.isKey)
{
newActionKey = e.keyCode;
PlayerPrefs.SetString("ActionKey", (e.keyCode.ToString()));
actionKeyText.text = "Action Key:" + newActionKey;
askKeyPanel.gameObject.SetActive(false);
}
}
}
}
Now I create a variable in the player code using this function:
public void UpdateActionKey()
{
actionKeyStr = PlayerPrefs.GetString("ActionKey", "E");
actionKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), actionKeyStr);
}
And I call that variable like this: Input.GetKey(actionKey))
Works like a charm!
The "E" key is a standard action key in most First Person games. The game jam version of Plaguebearer hard coded the "E" key for all actions. I had made a note to set this to a variable which can be set by the player and also got some playtest feedback that people wanted to set their own action key. So, I went looking for the solution to this and here is what I came up with...
The Approach:
1. In the controls setting menu, have a UI button to change the action key
2. On pressing the button, show a panel with instructions to "Press Any Key"
3. Don't do anything until a key is pressed
4. Read keypress
5. Write keypress to playerprefs
6. Read keycode from playerprefs as a variable and set in player script
Here is the code for the settings menu. This sets the key pressed into player prefs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controlsSettings : MonoBehaviour {
public Text actionKeyText;
public string currentActionKey;
public GameObject askKeyPanel;
public bool askKeyUp;
public KeyCode newActionKey;
// Use this for initialization
void Start () {
askKeyUp = false;
currentActionKey = PlayerPrefs.GetString("ActionKey","E");
actionKeyText = actionKeyText.GetComponent<Text>();
actionKeyText.text = "Action Key:" + currentActionKey;
askKeyPanel.gameObject.SetActive(false);
}
public void changeActionKey()
{
askKeyPanel.gameObject.SetActive(true);
askKeyUp = true;
}
void OnGUI()
{
if (askKeyUp)
{
Event e = Event.current;
if (e.isKey)
{
newActionKey = e.keyCode;
PlayerPrefs.SetString("ActionKey", (e.keyCode.ToString()));
actionKeyText.text = "Action Key:" + newActionKey;
askKeyPanel.gameObject.SetActive(false);
}
}
}
}
public void UpdateActionKey()
{
actionKeyStr = PlayerPrefs.GetString("ActionKey", "E");
actionKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), actionKeyStr);
}
And I call that variable like this: Input.GetKey(actionKey))
Works like a charm!
Comments
Post a Comment