- Home /
 
               Question by 
               danlinenberg · Jul 16, 2013 at 01:44 PM · 
                variablechangefixedupdate  
              
 
              Effective way to change var after buttondown
Hi, I have an object that constantly rotates around its Y axis. I want to change the direction of the rotation each time I press (either tap/hold) a button. The direction of the rotation is fixed by an INT variable that is changed from 1 to 0 (1=right, 0=left).
I've written a function that is called inside FixedUpdate, and it basically works, but sometimes it "misses" and failed to change the variable.
 private int RotationDirection;
 
     void Start () {
 
     RotationDirection=1;
        }
 
     void FixedUpdate () {
 
         switchRotation(); 
         }
 
 
 
 void switchRotation(){
 
                     if(RotationDirection==0 && Input.GetButtonDown("Fire1")){
                         RotationDirection=1;
                         
                         }
                     
 else{
                         if(RotationDirection==1 && Input.GetButtonDown("Fire1")){
                         RotationDirection=0;
                     
         }
     }
                 
 }
 
 
 
Is there a way to change that variable in a "Full Proof" way? 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by amphoterik · Jul 16, 2013 at 01:49 PM
Hello, first off, I wouldn't put this code in fixedupdate. That is for very fast, physics oriented calculations. Instead, I would write the code like:
 void Update()
 {
     if(Input.GetButtonDown("Fire1")){
         if(RotationDirection == 0)
             RotationDirection = 1;
         else
             RotationDirection = 0;
     }
 }
This will work. The IF statement is also simplified to avoid your "skipping" issue
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                