- Home /
Input.GetKeyDown gets registered twice in row
Hello, am trying to a make a turn based RPG and currently working on the battle UI. The player gets a first menu to pick his general move from (Attack, item, talk, run), if he chooses attack it leads him to a menu with the different moves the character has. My problem is that (Input.GetKeyDown(KeyCode.W) seems to instantly pick the attack menu AND the first move on the list in the same button press. thanks in advance.
private void Update()
{
if (state == BattleState.PlayerAction)
{
HandlePlayerAction();
}
if (state == BattleState.PlayerMove)
{
HandlePlayerMove();
}
}
private void HandlePlayerAction()
{
int prevSelection = currentaction;
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (currentaction < 3)
currentaction++;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (currentaction > 0)
currentaction--;
}
if (prevSelection != currentaction)
hud.UpdateActionSelection(currentaction);
if (Input.GetKeyDown(KeyCode.W))
{
switch (currentaction)
{
case 0:
//Attack
PlayerMove();
break;
case 1:
;
//Item
break;
case 2:
//Talk
break;
case 3:
//Rung
break;
default:
Debug.Log("Bug: selectionUI");
break;
}
}
}
private void HandlePlayerMove()
{
int prevSelection = currentmove;
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (currentmove < playerUnit1.Monster.Moves.Count - 1)
currentmove++;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (currentmove > 0)
currentmove--;
}
if (prevSelection != currentmove)
hud.UpdateMoveSelection(currentmove, playerUnit1);
if (Input.GetKeyDown(KeyCode.W))
{
hud.EnableMoveSelector(false);
hud.EnableDialogueText(true);
StartCoroutine(PerformMove());
}
}
What is the definition of BattleState? What does PlayerMove() look like?
My guess is that PlayerMove() sets state = BattleState.PlayerMove.
But we can't really know without code.
That would be my guess too. Solution would be to change Update()
from a series of if
blocks to a switch
statement. Chances are that this a a good idea anyway (i.e. that you don't want it to be possible for multiple states to be processed in the same frame).
Answer by Marioooo · Mar 16 at 02:47 PM
i reccomend you for this case to use the new input system instead of manual input checking. That way you can treat any inpus as events and avoid to use the update system to handle input.
Your answer
Follow this Question
Related Questions
Side battle tutorial using 3d objects 0 Answers
A list of enemies and players speed to determine turn order 2 Answers
How do I do a for loop the won't go around everything? 1 Answer
Turn Base Battle Simulation/Autobattle 0 Answers
How can I pause one object's Update() until another class has performed its task? 2 Answers