i wanna do something like this in c# on unity engine:
int main()
{
char ch;
ch=cin.get();
if(ch==‘a’){
.
.
.
}
cin.ignore();
cin.ignore();
return 0;
}
i don’t want player see which key he pressed down on the screen. (so i don’t want use Input Field)
thank you in advance
The closest equivalent would be Input.anyKey and Input.inputString. Since Unity is a real time engine you poll these each frame and check against your desired outcome, hence ‘a’.
first of all thanks to @hexagonius for the answer.
i found 2 ways of saving player’s input to a variable without Input Field that i wanna share them with you:
- using Input.inputString (as @hexagonius mentioned):
example:
void Update ()
{
foreach (char c in Input.inputString)
{
if (c == 'a') { ... }
}
}
in my experience of using Input.inputString i realized that variable should be initialized within foreach parentheses. Therefore variable is only defined within foreach brackets.
Input.inputString on unity docs
- using Event:
Example:
private void OnGUI()
{
Event e = Event.current;
if (e.character == 'a') { ... }
}
Event on unity docs
in my experience of using this tow methods, using first method is better for resources.