- Home /
 
               Question by 
               JG387 · Apr 07, 2014 at 04:47 PM · 
                updateif-statements  
              
 
              If statements in update
Hi, I just started to learn programming and I have so many questions. I understand that making to many if statements in update is kind of a no no. So if I want to listen after key presses I could just do many if statements in update like this:
 void Update () 
     {            
         if (Input.GetKeyDown ("space"))
         {
             call this funktion1();
         }
         if (Input.GetMouseButtonDown (0))
         {
             call this funktion2();
         }
         if (Input.GetMouseButtonDown (1))
         {
             call this funktion3();
         }
 and so on.......
 }
How do I do this in an acceptable manner? Would it be better to do:
 void Update () 
 {            
     if (Input.anyKeyDown)
     {
         StopCoroutine("CheckButtonDown");
         StartCoroutine("CheckButtonDown");
     }
 }
 
 IEnumerator CheckButtonDown()
 {
 
     if (Input.GetKeyDown ("space")) 
     {
         call this funktion1();
         yield return null;
     }
         
     if (Input.GetMouseButtonDown (0))
     {
         call this funktion2();
         yield return null;
     }
         
     if (Input.GetMouseButtonDown (1))
     {
         call this funktion3();
         yield return null;
     }
 
Is this better? Or even worse?
Or where do I put the if statement to check if I should move the camera, eg if its near the edge of the screen. If I want to be able to move it while Time.timeScale = 0 Eg:
 void update()
 {
         if(Input.mousePosition.x <= 0) 
         transform.position -= transform.right * constant;
 
         if(Input.mousePosition.x >= Screen.width) 
         transform.position += transform.right * constant;
                     
         if (Input.mousePosition.y <= 0) 
         transform.position -= transform.forward * constant;
                     
         if(Input.mousePosition.y >= Screen.height) 
         transform.position += transform.forward * constant;
 }
               Comment
              
 
               
              Answer by perchik · Apr 07, 2014 at 04:49 PM
The first ways the best... I know that the Input.Get* calls are always evaluated on update and would probably yield bad/unreliable results in a coroutine
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                