I’m thinking of adding this in game. Let’s say in a scene player randomly types a word while playing is it possible to check whether the player has typed a required word or a string?
Hi @galahad_here, maybe you can get the keyboard input with Input.inputString.
If you are using the new Input System, then you can use the Keyboard.onTextInput event.
Example from the docs:
// Let's say we want to do a typing game. We could define a component
// something along those lines to match the typed input.
public class MatchTextByTyping : MonoBehaviour
{
public string text
{
get => m_Text;
set
{
m_Text = value;
m_Position = 0;
}
}
public Action onTextTypedCorrectly { get; set; }
public Action onTextTypedIncorrectly { get; set; }
private int m_Position;
private string m_Text;
protected void OnEnable()
{
Keyboard.current.onTextInput += OnTextInput;
}
protected void OnDisable()
{
Keyboard.current.onTextInput -= OnTextInput;
}
private void OnTextInput(char ch)
{
if (m_Text == null || m_Position >= m_Text.Length)
return;
if (m_Text[m_Position] == ch)
{
++m_Position;
if (m_Position == m_Text.Length)
onTextTypeCorrectly?.Invoke();
}
else
{
m_Text = null;
m_Position = 0;
onTextTypedIncorrectly?.Invoke();
}
}
}