- Home /
 
               Question by 
               Raul_tecar · Oct 12, 2014 at 04:44 AM · 
                returnsteering  
              
 
              How to make car wheels return to initial position?
I have a script that gets input from a Joystick. It works great for turning the wheels left or right, but I want the wheels to return to the initial position when I release the joystick. Any ideas?
if (joySteer.position.x != 0) {
         float i = joySteer.position.x;                                                    
         
         if(i < 0){
             if(wheelAngle > -maxAngle){                    
                 wheelAngle -= turnSpeed*Time.deltaTime;    
             }else{
                 wheelAngle = -maxAngle;            
             }
         }
         
         if(i > 0){
             if(wheelAngle < maxAngle){                        
                 wheelAngle += turnSpeed*Time.deltaTime;    
             }else{
                 wheelAngle = maxAngle;                
             }
         }
         
         frontRight.steerAngle = wheelAngle;    
         frontLeft.steerAngle = wheelAngle;                
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by NoseKills · Oct 12, 2014 at 09:45 PM
 if (joySteer.position.x != 0) {
 
        float i = joySteer.position.x;                                                  
  
        if(i < 0)
        {
             if(wheelAngle > -maxAngle){                    
                 wheelAngle -= turnSpeed*Time.deltaTime;    
             }else{
                 wheelAngle = -maxAngle;            
             }
        }
        else if(i > 0) // use "else if" because if i was less than 0, how could it also be more than 0? 
        {
             if(wheelAngle < maxAngle){                      
                 wheelAngle += turnSpeed*Time.deltaTime;    
             }else{
                 wheelAngle = maxAngle;              
             }
         }
         else // i == 0 means steering is centered
         {
              // bring wheelAngle closer to 0 to straighten wheels
              float centeringAmount = turnSpeed*Time.deltaTime;
              if (wheelAngle > 0) 
              {
                  wheelAngle -= centeringAmount;
                  if (wheelAngle < 0) wheelAngle = 0; // prevent correcting too much
              }
              else if (wheelAngle < 0)
              {
                  wheelAngle += centeringAmount;
                  if (wheelAngle > 0) wheelAngle = 0;
              } 
         }
  
         frontRight.steerAngle = wheelAngle;    
         frontLeft.steerAngle = wheelAngle;              
     }
Your answer
 
 
             Follow this Question
Related Questions
vehicle decceleration and angle reseting 1 Answer
Setting up Unity Steer 0 Answers
Return to initial position and rotation 0 Answers
How to make a steering wheel that rotates when wheels do? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                