- Home /
more than one key to do something
hello ,i want idea when i create game , i want more keys to do type hit,street fighter method press s then a then d to do animation
So, what's stopping you? You have not asked a question.
Answer by Trevdevs · Jun 28, 2017 at 07:15 PM
Lets have a lesson. please try at least doing a google search before you ask a question next time.
Input.GetKey() Means if such a key is being held https://docs.unity3d.com/ScriptReference/Input.GetKey.html
Input.GetKeyDown() Means if such a key is pressed https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
I've never played street fighter but if it goes something like where you HOLD s and HOLD a then press d is does something different or if its you press every key at the same time
HOLD s HOLD a PRESS d
if(Input.GetKey(KeyCode.S)) //S is being held
{
if(Input.GetKey(KeyCode.A)) //A is being held
{
if(Input.GetKeyDown(KeyCode.D)) //D is pressed
{
//do something...
}
}
}
PRESS s PRESS a PRESS d
if(Input.GetKeyDown(KeyCode.S)) //Pressing all 3 S A D keys at the same time
{
if(Input.GetKeyDown(KeyCode.A))
{
if(Input.GetKeyDown(KeyCode.D))
{
//Do something
}
}
}
Answer by DominikKin · Jun 28, 2017 at 07:20 PM
Wouldn't combining it make more sense? Is there a difference to these 2? Only asking as a newb
if(Input.GetKeyDown(KeyCode.S) && Input.GetKeyDown(KeyCode.A) && Input.GetKeyDown(KeyCode.D) )
{
//Do something
}
I think it's really a matter of preference here. The behavior is the exact same. When you '&&' conditions together the control exits the tests once the first false is found. (this is good if you want to test (object != null && object.something == x), saves you from an error when object is null. So it's the same as the nested example. I like your example better, though. it just looks cleaner to me.
Answer by Jwizard93 · Jun 28, 2017 at 09:06 PM
Build a Moore type Finite State Machine. Example:
When nothing has been pressed you are in state 0. If you press 'A' you are in state 1. If you now press 'B' you go to state 2 which triggers the AB combo. If from state 1 you press 'C' then you go to state 3 which triggers the AC combo. It's as complex as you make it. Make sure to return to state 0 eventually or if you press a key not part of a combo.
Answer by ahmad-hadia · Jun 29, 2017 at 10:38 PM
thanks all , in this time i do not want to make game like this ,it is fighter game , and more than 30 move list ,so must have more keys,but i know i have it in unity , thanks again for all