How can I use on another scene the key bindings?
Hi I have two scenes. On the first scene I can change my controlls. For that I have made an Empty Object with a KeyBindScript. In the Script there is a Dictionary with keybinds. On the second scene there is my player with the movement script. So my Question is, how can I use the keys like Input.GetKeyDown(keys["Up"]) in my second scene for my player?
Scene 1:
public class KeyBindScript : MonoBehaviour {
public Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
public Text Up, Left, Down, Right, Attack;
private GameObject currentKey;
private Color32 normal = new Color32(39, 171, 249, 255);
private Color32 selected = new Color32(239, 116, 36, 255);
void Awake()
{
DontDestroyOnLoad(this);
}
// Use this for initialization
void Start () {
keys.Add("Up", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Up", "W")));
keys.Add("Down", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Down", "A")));
keys.Add("Left", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Left", "S")));
keys.Add("Right", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Right", "D")));
keys.Add("Attack", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack", "G")));
Up.text = keys["Up"].ToString();
Down.text = keys["Down"].ToString();
Left.text = keys["Left"].ToString();
Right.text = keys["Right"].ToString();
Attack.text = keys["Attack"].ToString();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(keys["Up"]))
{
//move
}
if (Input.GetKeyDown(keys["Down"]))
{
//move
}
if (Input.GetKeyDown(keys["Left"]))
{
//move
}
if (Input.GetKeyDown(keys["Right"]))
{
//move
}
if (Input.GetKeyDown(keys["Attack"]))
{
//move
}
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene(0);
}
}
public void defaultButton()
{
keys["Up"] = KeyCode.W;
keys["Down"] = KeyCode.S;
keys["Left"] = KeyCode.A;
keys["Right"] = KeyCode.D;
keys["Attack"] = KeyCode.G;
Up.text = keys["Up"].ToString();
Down.text = keys["Down"].ToString();
Left.text = keys["Left"].ToString();
Right.text = keys["Right"].ToString();
Attack.text = keys["Attack"].ToString();
}
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 = selected;
}
public void SaveKeys()
{
foreach(var key in keys)
{
PlayerPrefs.SetString(key.Key, key.Value.ToString());
}
PlayerPrefs.Save();
SceneManager.LoadScene(1);
}
}
Scene 2:
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if(movement_vector != Vector2.zero)
{
anim.SetBool("isWalking", true);
anim.SetFloat("x", movement_vector.x);
anim.SetFloat("y", movement_vector.y);
} else
{
anim.SetBool("isWalking", false);
}
rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
Resident Evil 4 Style Aiming Window - Rotational Problem When Aiming 1 Answer
Lerp ignores collision and allows player to move through a wall. 0 Answers
Why does my player move differently than i command? 0 Answers
movement control for IOS 0 Answers
when my character rotates the controls of the initial rotation stay and not change with it 0 Answers