D
D
Da_Vinchi_Official2021-12-08 01:51:56
C++ / C#
Da_Vinchi_Official, 2021-12-08 01:51:56

How can i convert if?

How can I and can I even convert a lot of if with (Input....) Example:

int i;

void example ()
{
   if(Input.GetKeyDown(KeyCode.1){
      i = 1;
   }
   if(Input.GetKeyDown(KeyCode.2){
         i = 2;
      }
   if(Input.GetKeyDown(KeyCode.3){
         i = 3;
      }
}

More efficient code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-12-08
@Da_Vinchi_Official

In the general case, no - anyway, in some place you will be called by Input.GetSomethingThere.
And through the chain of ifs, there is the most effective option (unless, of course, you check only those buttons that make sense to check)
But it seems like some new input system has appeared in Unity, which should be more flexible:
https://gamedevbeginner. com/input-in-unity-made-ea...

G
GFX Data, 2021-12-08
@ShockWave2048

List<KeyCode> keysNums = new List<KeyCode> { KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4 };

void Update()
{
        foreach (var key in keysNums)
        {
            if ( Input.GetKeyDown(key))
            {
                int i = keysNums.IndexOf(key)];
                ....
            }
        }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question