- Home /
How do I make a program that first press button 1 release it and then press button 2 in order to walk?
I want to make the player walk by first pressing one button, let's say "A" then releasing that button and after that pressing another let's say "D". so press "A" release "A" press "D", is one step. Then releasing "D" and pressing "A" would be a second step. This continues until the player stops the sequence.
If the player wait too long the chain should stop. that is just because the player presses "D" and the last button pressed was "A" shouldn't make the character walk.
So am I understanding this correctly. "A" release, then "D" is - say left foot moving forward, release "D" then press "A" would then move the right foot forward? Seems like a tiring way for the player to make his character walk. but...not my place to comment on gameplay. Tried this out and it seems to work although the Debug prints out "left" or "right" twice for each step. Not sure about the the player waiting to long. A timer or coroutine of some sort. here is a little something .far from perfect but it's a start
public bool leftFoot;
public bool rightFoot;
public float walkTimer = 1.5f;
public bool tooLong;
public bool walking;
void Start(){
leftFoot = false;
rightFoot = false;
}
void Update(){
if(walking == true)
walkTimer -= Time.deltaTime;
if (leftFoot == true || rightFoot == true)
walking = true;
if (walkTimer <= 0 ) {
tooLong = true;
leftFoot = false;
rightFoot = false;
walking = false;
walkTimer = 0;
StartCoroutine(walkRestart());
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A)&& tooLong == false){
leftFoot = true;
rightFoot = false;
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D)&& tooLong == false){
leftFoot = false;
rightFoot = true;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && leftFoot == true && tooLong ==false){
Debug.Log ("left");
walkTimer = 1.5f;
//code for left foot forward
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && rightFoot == true && tooLong ==false){
Debug.Log ("right");
walkTimer = 1.5f;
//code for rightfoot forward
}
}
private IEnumerator walkRestart(){
yield return new WaitForSeconds(1);
walkTimer = 1.5f;
tooLong = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612160842im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Play Sound on Button Highlight 2 Answers
wait for 3 seconds button?? 3 Answers
Numpad Keypress not being registered on mobile phone. 0 Answers
Putting a delay in a button OnClick ? (Javascript) 3 Answers