- Home /
The question is answered, right answer was accepted
Check if pressed key exists inside an array of KeyCodes
Is there anything like this for KeyCode?
using UnityEngine;
void LateUpdate ()
{
KeyCode[] keyCheck = new KeyCode[4];
if (Input.anyKeyDown == keyCheck[2])
Debug.Log(Input.inputString);
}
this?
void LateUpdate ()
{
foreach ($$anonymous$$eyCode item in keyCheck )
{
if (Input.Get$$anonymous$$eyDown(item))
Debug.Log(Input.inputString);
}
}
Answer by Addyarb · Jun 02, 2017 at 03:25 AM
You may be looking for something like this. Please be sure to change the declaration (the first line) to the Start function!
void Update()
{
KeyCode[] keycodes = new KeyCode[]{ KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D };
for (int i = 0; i < keycodes.Length; i++) if (Input.GetKeyDown(keycodes[i])) Debug.Log("Pressed " + keycodes[i]);
}
What do you mean by "...change the [...] first line [...] to Start function!"? And, by the way, that worked, thank you!
Good question. So this line:
$$anonymous$$eyCode[] keycodes = new $$anonymous$$eyCode[]{ $$anonymous$$eyCode.W, $$anonymous$$eyCode.A, $$anonymous$$eyCode.S, $$anonymous$$eyCode.D };
Is relatively 'expensive,' or in other words - it will cause a significant lag in your game if you keep it in the 'void Update' function/method.
To solve this, move it to the top of your script. You can define the keys you want pressed there, and not have to tell your script every frame. The reason is that it only needs to be called once, whereas the script should check every frame (hence Update) whether or not you are pressing one of those keys.
Your final script, therefore, should look like this:
$$anonymous$$eyCode[] keycodes = new $$anonymous$$eyCode[]{ $$anonymous$$eyCode.W, $$anonymous$$eyCode.A, $$anonymous$$eyCode.S, $$anonymous$$eyCode.D };
void Update()
{
for (int i = 0; i < keycodes.Length; i++) if (Input.Get$$anonymous$$eyDown(keycodes[i])) Debug.Log("Pressed " + keycodes[i]);
}
Oh you meant that... As I said in a comment at the answer below, I was way too hurried to think about code shaping, so I wrote it the fastest legible way... Sorry about that, but, anyway, thanks, it was clearifying!
And about the Update thing, I keep alternating, at some scripts I use Update, and at others I use LateUpdate.
Again, thanks!
@Addyarb Please, check this question, 'cause I guess you might have an answer to it: http://answers.unity3d.com/questions/1360956/trouble-with-fieldinfosetvalue-and-arrays.html
Answer by Brogan89 · Jun 02, 2017 at 01:49 AM
Right now you are creating an array of KeyCode types. Which has been initialised but not populated. If I understand what you are trying to do i think you'd need to do this
KeyCode[] keyCodes = new KeyCode[] { KeyCode.A, KeyCode.B }; if (Input.GetKeyDown(keyCodes[2])) { Debug.Log(Input.inputString); }
That declarations above, in my example, are just what they are: EXA$$anonymous$$PLES. I did it that way 'cause I was way to busy to keep pressing space to format my text. If I were at Visual Studio, that's what they would look like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$yExample : $$anonymous$$onoBehaviour
{
[SerializeField] private $$anonymous$$eyCode[] keyCheck = new $$anonymous$$eyCode[4]; //The keys are set at Inspector
void LateUpdate ()
{
if (Input.any$$anonymous$$eyDown == keyCheck[2]) //Now I know what is wrong with this declaration, so never $$anonymous$$d.
Debug.Log(Input.inputString);
}
}
Anyway, thanks for spending your time to answer me!