- Home /
Changing controls in game, after application has launched
Hi, I want to know if it is possible to change control inputs in-game (after the application has launched). For example, I could have a "Change Controls" menu in the main menu, where the user can click on a box, for example "Move forward", press what button they want to move forward, and have that button be saved as the move forward key.
Answer by robertbu · Jan 31, 2013 at 01:51 AM
One solutions is to put your keycode checks into variables. Something like (in C#):
KeyCode kcForward = KeyCode.UpArrow;
if (Input.GetKeyDown (kcForward)) { // Do the forward stuff }
Later you can reassign the value.
kcFordward = KeyCode.W;
So now the 'W' key will move forward. Of course you'll have to write the interface that allows the user to change the keys mappings for your game.
I was wondering if there was an easier way to have the user change the key mappings than hard coding an if statement for every single key on the keyboard.
For example, ins$$anonymous$$d of doing
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
kcForward = $$anonymous$$eyCode.W
for every single $$anonymous$$eyCode there is, is there a function that returns the key that the user pressed as a $$anonymous$$eyCode variable?
If not, I guess I'll have to write the interface myself.
You can see all the keys pressed. Here is an example from the script reference:
// Detects keys pressed and prints their keycode
function OnGUI() {
var e : Event = Event.current;
if (e.is$$anonymous$$ey) {
Debug.Log("Detected key code: " + e.keyCode);
}
}
Or you could create an array (or a class) that remaps as needed:
if (Input.Get$$anonymous$$eyDown (Remap[$$anonymous$$eyCode.UpArrow])) { // Do the forward stuff }
Answer by dehow64 · Nov 06, 2013 at 01:43 AM
I tried using the solution provided and I still can't get my player to move. Here's what I put
enter code here
using UnityEngine;
using System.Collections;
public class Change_Controls : MonoBehaviour {
public string moveForward = "x";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
KeyCode kcForward = KeyCode.X;
if (Input.GetKeyDown (kcForward)) { // Do the forward stuff }
kcForward = KeyCode.X;
}
}
}
You should use Input.Get$$anonymous$$ey ins$$anonymous$$d of Input.Get$$anonymous$$eyDown.