Letters typing for object controls ,Letter tying for controls
Hello there.
I want to make my game control to that the player must type the correct word of direction in order for the object to move in that intended direction. For example, the object will only move up if the player typed "up" correctly.
This is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Mov : MonoBehaviour { List letters = new List(); string[] mywords = new string[] { "up" }; string myword; //public Text text; public GameObject player; bool mup;
void Start()
{
myword = mywords[Random.Range(0, mywords.Length - 1)];
mup = false;
//text.text = myword;
}
void Update()
{
if(mup == true)
{
player.transform.position = new Vector3(0, 0, 0);
}
}
void onGui()
{
if (Event.current.type == EventType.KeyDown && Event.current.keyCode != KeyCode.None)
{
Debug.Log("ok");
letters.Add(Event.current.keyCode.ToString());
for (int i = 0; i <= letters.Count - 1; i++)
{
if (letters[i] == myword.Substring(i, 1))
{
Debug.Log("yay you typing ");
//text.color = Color.green;
if (letters.Count == myword.Length)
{
mup = true;
letters.Clear();
//myword = mywords[Random.Range(0, mywords.Length - 1)];
//text.text = myword;
//text.color = Color.white;
}
}
else
{
Debug.Log("boo you suck");
letters.Clear();
//text.color = Color.red;
}
}
}
}
},I want to control my object through typing the correct word for the intended direction, such as the object will only go up if the player types "up" correctly.
this is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Mov : MonoBehaviour { List letters = new List(); string[] mywords = new string[] { "up" }; string myword; //public Text text; public GameObject player; bool mup;
void Start()
{
myword = mywords[Random.Range(0, mywords.Length - 1)];
mup = false;
//text.text = myword;
}
void Update()
{
if(mup == true)
{
player.transform.position = new Vector3(0, 0, 0);
}
}
void onGui()
{
if (Event.current.type == EventType.KeyDown && Event.current.keyCode != KeyCode.None)
{
Debug.Log("ok");
letters.Add(Event.current.keyCode.ToString());
for (int i = 0; i <= letters.Count - 1; i++)
{
if (letters[i] == myword.Substring(i, 1))
{
Debug.Log("yay you typing ");
//text.color = Color.green;
if (letters.Count == myword.Length)
{
mup = true;
letters.Clear();
//myword = mywords[Random.Range(0, mywords.Length - 1)];
//text.text = myword;
//text.color = Color.white;
}
}
else
{
Debug.Log("boo you suck");
letters.Clear();
//text.color = Color.red;
}
}
}
}
}
Answer by Hellium · Sep 16, 2019 at 07:41 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mov : MonoBehaviour
{
private Dictionary<string, Vector3> directions = new Dictionary<string, Vector3> {
{ "up", Vector3.up },
{ "down", Vector3.down },
{ "left", Vector3.left },
{ "right", Vector3.right }
};
private string input = "";
// Update is called once per frame
void Update()
{
char letter;
if (IsTypingLetter(out letter))
{
input += letter;
if (!CheckInput())
{
Debug.LogWarning("Fail");
input = letter.ToString();
CheckInput();
}
}
}
private bool CheckInput()
{
KeyValuePair<string, Vector3> direction;
bool isTypingDirection = IsTypingDirection(input, out direction);
if (isTypingDirection)
{
if (string.Equals(direction.Key, input))
{
Debug.Log("Direction correct. Moving object " + input);
transform.position += direction.Value;
input = "";
}
else
{
Debug.Log("Go on! You are typing " + input);
}
}
return isTypingDirection;
}
private bool IsTypingLetter(out char letter)
{
if(Input.inputString.Length == 1)
{
letter = Input.inputString.ToLower()[0];
if(letter >= 'a' && letter <= 'z')
{
return true;
}
}
letter = '\0';
return false;
}
private bool IsTypingDirection(string input, out KeyValuePair<string, Vector3> direction)
{
foreach (KeyValuePair<string, Vector3> dir in directions)
{
if (dir.Key.StartsWith(input))
{
direction = dir;
return true;
}
}
direction = new KeyValuePair<string, Vector3>(null, Vector3.zero);
return false;
}
}