- Home /
Button remapping.
The script i made doesn't record what the person presses after pressing the key. Thats all I really know. but you guys can look at it to see if theres any other things i could do..</p> <pre><code> var inputkey1 = "w"; var inputswitch = false; var inputkey1valid = true; var inputkeychanged = false; var inputkey1a = "w"; var inputkey1b = "a"; var inputmessage1 = "1111111113022"; var inputkey2 = "2"; var inputmessage2 = "222223022"; var inputkey1false = false; private var gt: GUIText; function Start () { gt = GetComponent(GUIText); if( gt == null ) Debug.Log("No GUIText component found"); } function Update () { //to allow changing of keys ((press before remapping is taking place)) if(Input.GetKeyDown("")) { inputswitch = true; inputkey1valid = false; inputkey1false = true; }
//if you pressed the key of the choice to change and you pressed ` before change the inputkey to the recorded key
//and reset inputswitching to false so it works and stays that variable.
if(Input.GetKeyDown(inputkey1) && inputswitch && inputkey1false)
{
var inputkey1b = Input.GetKeyDown;
inputswitch = false;
inputkeychanged = true;
inputkey1valid = true;
inputkey1false = false;
}
if(inputkey1false)
{
inputkey1valid = false;
}
//if the keys are opted to change then its the changed/recorded key
if(inputkeychanged)
{
inputkey1 == inputkey1b;
inputkey1valid = true;
}
//reset the keys if = is pressed.
if(Input.GetKeyDown("="))
{
inputswitch = false;
inputkeychanged = false;
inputkey1 == inputkey1a;
inputkey1valid = true;
}
if(Input.GetKeyDown(inputkey1) && inputkey1valid)
{
gt.text = inputmessage1;
}
if(Input.GetKeyDown(inputkey2))
{
gt.text = inputmessage2;
}
}
if it isn't absolutely necessery I wouldn't use an ingame console to change keys, I would use the input manager under project in the edit menu.
Answer by AngryOldMan · Mar 14, 2011 at 03:04 AM
I don't know how but this script doesn't show any compiler errors. It still doesn't seem to do anything though. Like Myth says is there any specific need to remap the keys at runtime? If you just want to change the keys you can go into "edit ~> project settings ~> input" and specify the keys and their names (which is the method used for calling them) In there you can set an axis' positive and negative buttons (or just a buttons positive if its only an on/off button) you can also set alternative buttons (so effectively setting two sets of keys for the same input) if for instance with movement you prefere to use WASD and a friend preferes the directional keys it is handy to set alternative keys.
If however you want to press a button during the game and completely change a control set may I suggest still changing the inputs via project settings but setting two completely different sets of keys, then creating a script which on a certain key press, disables one set of controls and enables the other. Which can be done all in the same script with a simple if variable, for instance
var ControlSet1 = true;
function Update () { if (Input.GetButtonDown ("ControlChange")) { ControlSet1 = false; } if (ControlSet1 == true) { //define the default controls for example moveDirection = Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0); moveDirection = transform.TransformDirection(moveDirection);
} if (ControlSet1 == false) { //define controls that you have switched to for example moveDirection = Vector3(Input.GetAxis("CSTHorizontal"),Input.GetAxis("CSTVertical"),0); moveDirection = transform.TransformDirection(moveDirection);
} }
CTS stands for Control Set Two but this is just an example you would have define the exact naming convention in the input settings then adapt your script to suit.
Your answer