La domanda ha avuto risposta, è stata accettata la risposta giusta
Double input.GetKey conditions accuracy
My apologies if this is been already asked but I searched and I found nothing. I'm using these conditions to manage player's facing direction:
if (Input.GetKey (KeyCode.UpArrow)) {
transform.forward = new Vector3 (0, 0, -1);
direction = "N";
} else if (Input.GetKey (KeyCode.DownArrow)) {
transform.forward = new Vector3 (0, 0, 1);
direction = "S";
} else if (Input.GetKey (KeyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 0);
direction = "W";
} else if (Input.GetKey (KeyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, 0);
direction = "E";
}
if (Input.GetKey (KeyCode.UpArrow) && Input.GetKey (KeyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, -1);
direction = "NE";
} else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) {
transform.forward = new Vector3 (-1, 0, 1);
direction = "SE";
} else if (Input.GetKey (KeyCode.DownArrow) && Input.GetKey (KeyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 1);
direction = "SW";
} else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) {
transform.forward = new Vector3 (1, 0, -1);
direction = "NW";
}
I'm also storing the current direction in a variable. The problem is that there's a lack of accuracy if we talk about a double Input.GetKey condition, because the last of the two keys that gets left, gives me back the wrong value of the variable, even if the player is facing the currect diagonal direction (not always, often happens that the player doesn't stop in diagonal direction). I just want the two keys to give me back the right value and the right direction facing even if left at different times. How can I fix this problem?
Answer by kromenak · Aug 26, 2015 at 08:18 PM
Input.GetKey will constantly return either true or false, depending on whether the key is currently pressed or not. On the other hand, Input.GetKeyDown will only return true on the frame the key is initially pushed down, and Input.GetKeyUp will only return true on the frame the key is released.
It seems that what you really want here is to only check for a direction change when a key is pressed down, not when a key is released. As a result, you could maybe try something like this:
private void Update()
{
// Only refresh the direction when a key is initially pressed down.
if(Input.GetKeyDown(KeyCode.UpArrow)
|| Input.GetKeyDown(KeyCode.DownArrow)
|| Input.GetKeyDown(KeyCode.LeftArrow)
|| Input.GetKeyDown(KeyCode.RightArrow))
{
RefreshDirection();
}
}
private void RefreshDirection()
{
if (Input.GetKey (KeyCode.UpArrow)) {
transform.forward = new Vector3 (0, 0, -1);
direction = "N";
} else if (Input.GetKey (KeyCode.DownArrow)) {
transform.forward = new Vector3 (0, 0, 1);
direction = "S";
} else if (Input.GetKey (KeyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 0);
direction = "W";
} else if (Input.GetKey (KeyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, 0);
direction = "E";
}
if (Input.GetKey (KeyCode.UpArrow) && Input.GetKey (KeyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, -1);
direction = "NE";
} else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) {
transform.forward = new Vector3 (-1, 0, 1);
direction = "SE";
} else if (Input.GetKey (KeyCode.DownArrow) && Input.GetKey (KeyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 1);
direction = "SW";
} else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) {
transform.forward = new Vector3 (1, 0, -1);
direction = "NW";
}
}
By doing this, you check for a direction change when a key is pressed down, but if the player releases the down key while still holding the left key, it won't change the direction.
But there are some other things you may need to take into account. For example, what if a player releases the down key, and holds the left key, but the player's intention IS to change the direction to West? As a result, when a key is released, you may want a short delay, after which the direction actually will refresh, because it becomes clear that the player intention is to change the direction.
Now I get the opposite problem ins$$anonymous$$d. By doing this way, when I walk, if I leave one of the two arrows to move on a vertical or horizontal axis ins$$anonymous$$d of the diagonal axis, the player keeps facing the diagonal directions while walking vertical/horizontal.
EDIT: Sorry but I didn't see the last part of the message. I tried to set a delay. It seems to work. The direction is the right one in any case. I write here the code to know if it's really ok and in this case to help if someone will have my same question.
private void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)
|| Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.DownArrow)
|| Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftArrow)
|| Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.RightArrow)) {
RefreshDirection ();
} else {
Delay++;
}
if (Delay > 7.25f) {
RefreshDirection ();
}
}
public void RefreshDirection(){
Delay = 0f;
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
transform.forward = new Vector3 (0, 0, -1);
direction = "N";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow)) {
transform.forward = new Vector3 (0, 0, 1);
direction = "S";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 0);
direction = "W";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, 0);
direction = "E";
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
transform.forward = new Vector3 (-1, 0, -1);
direction = "NE";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow)) {
transform.forward = new Vector3 (-1, 0, 1);
direction = "SE";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
transform.forward = new Vector3 (1, 0, 1);
direction = "SW";
} else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
transform.forward = new Vector3 (1, 0, -1);
direction = "NW";
}
}
Follow this Question
Related Questions
I'd like my object to be active only when space key is NOT pressed 1 Answer
c# - Make something happen if I press a sequence of keys 4 Answers
Input.GetKey("Horizontal")... why its not working? 1 Answer
Set new default position each time? 1 Answer
Using one key only, change bool to true or false depending on it's current value 2 Answers