- Home /
i can assign all keys but the shift any ideas
so code works fully thats not problem no errors or anything but i cant assign a key to use shift all other keys work on keyboard and can be assigned
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class keysetting : MonoBehaviour {
private Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
public Text up, right, left, down, run, jump, interact, hideui;
public GameObject menu;
private bool disablemenu;
private GameObject currentKey;
private Color32 normal = new Color32(125, 3, 3, 255);
private Color32 slected = new Color32(239, 116, 36, 255);
public float speed = 5f;
public GameObject character;
//public CharacterController movementCode;
// Use this for initialization
void Start () {
//not saving
//working
keys.Add("Up", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Up", "W")));
keys.Add("Right", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Right", "D")));
keys.Add("Left", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Left", "A")));
keys.Add("Down", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Down", "S")));
keys.Add("Run", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Run", "Q")));
keys.Add("Jump", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Jump", "Space")));
keys.Add("Interact", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interact", "E")));
keys.Add("HideUI", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("HideUI", "Escape")));
up.text = keys["Up"].ToString();
right.text = keys["Right"].ToString();
left.text = keys["Left"].ToString();
down.text = keys["Down"].ToString();
run.text = keys["Run"].ToString();
jump.text = keys["Jump"].ToString();
interact.text = keys["Interact"].ToString();
hideui.text = keys["HideUI"].ToString();
//movementCode = character.GetComponent <CharacterController> ();
}
// Update is called once per frame
void Update () {
//Movement Keys
if (Input.GetKey(keys["Up"]))
{
//move action
character.transform.localPosition += character.transform.forward * speed * Time.deltaTime;
Debug.Log("Forword is pressed");
}
if (Input.GetKey(keys["Right"]))
{
character.transform.localPosition += character.transform.right * speed * Time.deltaTime;
//move action
Debug.Log("Right is pressed");
}
if (Input.GetKey(keys["Left"]))
{
character.transform.localPosition -= character.transform.right * speed * Time.deltaTime;
//move action
Debug.Log("Left is pressed");
}
if (Input.GetKey(keys["Down"]))
{
character.transform.localPosition -= character.transform.forward * speed * Time.deltaTime;
//move action
Debug.Log("Down is pressed");
}
if (Input.GetKey(keys["Run"]))
{
speed = 10f;
}
else
{
speed = 5f;
}
//MenuKeys
if (Input.GetKeyDown(keys["HideUI"]))
{
//menu action
disablemenu = !disablemenu;
menu.SetActive(disablemenu);
Debug.Log("HideUI is working");
}
}
private void moveForward(float speed)
{
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
private void moveBack(float speed)
{
transform.localPosition -= transform.forward * speed * Time.deltaTime;
}
private void moveRight(float speed)
{
transform.localPosition += transform.right * speed * Time.deltaTime;
}
private void moveLeft(float speed)
{
transform.localPosition -= transform.right * speed * Time.deltaTime;
}
void OnGUI()
{
if(currentKey != null)
{
Event e = Event.current;
if(e.isKey)
{
keys[currentKey.name] = e.keyCode;
currentKey.transform.GetChild(0).GetComponent<Text>().text = e.keyCode.ToString();
currentKey.GetComponent<Image>().color = normal;
currentKey = null;
}
}
}
public void ChangeKey( GameObject clicked)
{
if(currentKey != null)
{
currentKey.GetComponent<Image>().color = normal;
}
currentKey = clicked;
currentKey.GetComponent<Image>().color = slected;
}
public void SaveKeys()
{
foreach (var key in keys)
{
PlayerPrefs.SetString(key.Key, key.Value.ToString());
//saves each key
}
PlayerPrefs.Save();
}
}
Answer by Genei_180 · May 29, 2017 at 04:33 AM
Maybe you Could use
If(Input.GetKeyDown(keycode.shift){
//stuff you want to do
}
If you want to toggel with the shift key you would need a bool that you toggel Like:
Bool toggel;
If( ... ) {
toggel = !toggel;
}
I hope i could help you with my rather not so expert advice
May you have no Errors in your code
no the code is made to change a string into a keycode it allows players to have custom keys problem is shift cant be assigned nor the mouse keys
Because the $$anonymous$$eycode doesn't get Returned? Seems Like i have the same Question as You Sry man im Not the best Programmer but i don't get the Problem why shift or $$anonymous$$ouse would no work? Because $$anonymous$$eyCode.LeftShift or $$anonymous$$eycode.RightShift do exist Also do $$anonymous$$eyCode.$$anonymous$$ouse0 1 2 3 etc...
Sry but i dont get how your keys get Assigned? You klick on the Aktion you like to assign on the UI and the the Application listens for $$anonymous$$ey Inputs?
you can alternatively have a function that call the shift and you can get it to return the keycode that you want
that may work but wont that cause issues if i add more to the code?
well considering that you're just going to call a function that contains this. You will save yourself from writing lots of repetitive lines of code by simply calling the function
Answer by Killerbro389 · Jun 16, 2017 at 08:36 AM
Just saying the key for shift is KeyCode. LeftShift and KeyCode.RightShift.
Answer by DiGiaCom-Tech · Jun 16, 2017 at 05:40 AM
It's KeyCode.LeftShift or KeyCode.RightShift ;)
Code would be something like...
if (Input.GetKey(KeyCode.LeftShift ) && Input.GetKeyUp(KeyCode.A)) {
do whatever;
}
code has to turn that into string then back into keycode that is the problem it little harder but it allow for keybinding so every player has there own choices it hard code to do it works for every key except shift and mouse buttons which haven't figured out why
Answer by Killerbro389 · Jun 16, 2017 at 06:00 AM
@DiGiaCom-Tech yeah that works I think
But why are you using custom names for key codes. You could just use GetButtom ins$$anonymous$$d of Get$$anonymous$$ey; the name will be whatever you assign.
$$anonymous$$ost games these days use a combination of keys and buttons. In his case he is creating a user interface (UI) for mapping keyboard keys to game functions. This is so a player can map keys like I, $$anonymous$$, J, L to player movement vs. the standard W, S, A, D keys.
Yes, he could use Input.GetButton("Whatever") but this would not solve the remapping issue as now the Input item for "Whatever' still needs to have it's keys reassigned.
That being said, it is a better way to go as the game inputs are now named (like "$$anonymous$$oveLeft" or "FireWeapon") so you don't have to refactor code throughout your whole application when 'Ctrl-A' becomes 'Alt-B' ;)
I know most of us prefer to write our own code but in some cases it might be a better idea to look at an asset ... I use 'Rewired'. That is, why spend hundreds of hours creating a solution when there are several plugins available in the Asset Store. I do realize that cost might be a factor for some hobbyists but I'd rather focus my work on my game mechanics that foundation stuff like this ;)
[Asset Store - Rewired:] https://www.assetstore.unity3d.com/en/#!/content/21676
the issue is more so that the keybinding will play a much larger part in the game which is why its important that i can map the shift keys and the mouse keys I am not 100% sure why its not doing it but i may have no choice but to set them as unchangeable keys and simply allow task to still be mapped aka shift is always sprint key but you can also make it Q i really didn't want to go that route though i rather have all the keys be able to be used and mapped
Your answer
Follow this Question
Related Questions
Force UI Input Field Character limit with C Sharp 0 Answers
How to get Key with KeyCode.Question? 1 Answer
KeyCode to Ui ? for mobile 1 Answer
Unity 2d select game object with keyboard 2 Answers
Help with Virtual Keyboard 0 Answers