In my code I am trying to detect whether a key for example 'A' is pressed alone or is pressed with combination. I have different if statements for each. But when I press them together something weird happens. I think the problem is related to Input.Getkey and when I try to use Input.GeyKeyDown it becomes nearly impossible to press them both at the same time. How can I improve my code?
It seems a common problem but I am unable to comprehend it. I want my transform to move to different location when only A is pressed and to another different location when A is pressed in combination with W or S. The result I want is in the image. When I click a&&w then generate a random Vector3 between a range like this:
new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f));
Generating random location works perfectly but the only problem is when I try to press combination keys, first this runs
if (Input.GetKeyUp(KeyCode.W))
{
activePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(4.5f, 6f));
return activePos;
}
then this
if (Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.A)))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(2f, 4.5f));
return activePos;
}
and this
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f));
return activePos;
}
Whereas I want only the last one to run. I want only this code to run when w&&a are pressed together
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f));
return activePos;
}
This is the full method:
public static Vector3 GetTargetPosition()
{
/*
* near z = 1 - 2.5, key = s
* center z = 2 - 4.5, key = q
* far z = 4.5 - 6, key = w
* very far possibly outside z = 6 - 7.5
* left x = (-2.7) to -0.95, key = a
* center x = -0.95 to 0.95, key = q
* right x = 0.95 to 2.7, key = d
*/
if (Input.GetKeyUp(KeyCode.S))
{
activePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(1f, 2.5f));
return activePos;
}
else if (Input.GetKeyUp(KeyCode.W))
{
activePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(4.5f, 6f));
return activePos;
}
else if (Input.GetKeyUp(KeyCode.Q))
{
activePos = new Vector3(Random.Range(0.95f, -0.95f), 0, Random.Range(2f, 4.5f));
return activePos;
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(1f, 2.5f));
return activePos;
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
activePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(1f, 2.5f));
return activePos;
}
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(4.5f, 6f));
return activePos;
}
else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
{
activePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(4.5f, 6f));
return activePos;
}
else if (Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.A)))
{
activePos = new Vector3(Random.Range(-2.7f, -0.95f), 0, Random.Range(2f, 4.5f));
return activePos;
}
else if (Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.D)))
{
activePos = new Vector3(Random.Range(0.95f, 2.7f), 0, Random.Range(2f, 4.5f));
return activePos;
}
else
{
return new Vector3(0, 0, 0);
}
}
