- Home /
How to execute action with two keys when they are alredy tied to other actions?
Hi, I'm a beginner programmer. I'm making a player rotation input method and I'm having trouble with making a 45 degree rotation by pressing two keys (up and right for example). The trouble is that the gameplay (2d top down game) is sort of timed dexterity thing and it requires only one attempt at rotation, so if the player wants to rotate to 45 and presses up and right at once the program registers either up or right rotation first and only then the 45 degree middle position. I think that I need some kind of very short timer during which the program will check is some other key pressed instantly after and make the rotation and if it is not then it will only rotate according to the first key. I tried several stuff but i can't make it work. Thanks in advance for any help.
Here's an example of my code for rotation without the middle 45 degree position.
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
}
Answer by David009 · Dec 12, 2016 at 10:18 PM
Just to post a solution to this. This gets called when a player presses first key.
IEnumerator CheckForRightButton()
{
int counter = 0;
bool rotated = false;
//here i check for five frames did the player press another key after the first one
while (counter < 5)
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 45));
rotated = true;
Debug.Log("Up/Right");
yield break;
}
counter++;
yield return null;
}
//if just one key was pressed then this is executed
if (!rotated)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
Debug.Log("Up");
}
}
Answer by UnityCoach · Dec 09, 2016 at 11:38 AM
You want to exclude each others from the first conditions, like :
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && !Input.GetKeyDown(KeyCode.RightArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && !Input.GetKeyDown(KeyCode.UpArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && Input.GetKeyDown(KeyCode.UpArrow))
{
// third action
}
}
or use a mutual exclusive state (finite state) using an enum
public enum STATE {None, Up, Right, UpRight};
private STATE _state;
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && !Input.GetKeyDown(KeyCode.RightArrow))
{
_state = STATE.Up;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && !Input.GetKeyDown(KeyCode.UpArrow))
{
_state = STATE.Right;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && Input.GetKeyDown(KeyCode.UpArrow))
{
_state = STATE.UpRight;
}
switch (_state)
{
case STATE.Up :
transform.rotation = Quaternion.Euler (Vector3.Up);
break;
case STATE.Right :
transform.rotation = Quaternion.Euler (Vector3.Right);
break;
case STATE.UpRight :
transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 45));
break;
}
}
The problem is that Get$$anonymous$$eyDown lasts only for one frame, both keys are never going to be registered at the same frame, because it's nearly impossible to press them that evenly.
I ended up using a coroutine to check for another button, during five frames after the first one is pressed.
IEnumerator CheckForRightButton()
{
int counter = 0;
bool rotated = false;
//here i check for five frames did the player press another key after the first one
while (counter < 5)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 45));
rotated = true;
Debug.Log("Up/Right");
yield break;
}
counter++;
yield return null;
}
//if just one key was pressed then this is executed
if (!rotated)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
Debug.Log("Up");
}
}
You're right. You may also want to use Get$$anonymous$$ey() ins$$anonymous$$d of Get$$anonymous$$eyDown(). Get$$anonymous$$ey() is true every frame the key is pressed.
Your answer
Follow this Question
Related Questions
Input.GetButton not working 1 Answer
Distribute terrain in zones 3 Answers
NullRefrenceException with new input system 0 Answers