Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Steering Wheel Rotation

Discussion in 'Scripting' started by Ecocide, Mar 5, 2012.

Thread Status:
Not open for further replies.
  1. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    Hi folks,

    I'd like to implement a rotating steering wheel to the players joystick input.

    To be honest, I tried THREE HOURS to realize this simple feature, but I'm not really statisfied.

    On the first view this one seemed to work

    Code (csharp):
    1. public class SteeringWheelC : MonoBehaviour {
    2.    
    3.     public float maxAngle = 360f;
    4.  
    5.    
    6.     void Update() {
    7.        
    8.        
    9.        
    10.         float angleRotation = Input.GetAxisRaw("Horizontal") * maxAngle;
    11.        
    12.         transform.eulerAngles = new Vector3(angleRotation-90, 60, 90);
    13.        
    14.  
    15.  
    16.     }
    17. }
    But of course the other two axis doesn't move, so the car goes to the right and my steering wheel is just fixed on 90 and 60 degrees.
    transform.rotation is not really helpful either, because you can only set the speed of rotation.

    Could you please give me a hint or something? I don't expect someone to write a complete script. I just need some advice :-(

    Thanks.
     
  2. Sir-Tiddlesworth

    Sir-Tiddlesworth

    Joined:
    Oct 19, 2011
    Posts:
    908
    Why don't you try using animations?
     
    unity_c16k0QN5F3ekoA likes this.
  3. jeffmorris

    jeffmorris

    Joined:
    Nov 26, 2009
    Posts:
    144
    Try this:

    transform.eulerAngles = Vector3(0,0,Input.GetAxis("Horizontal")*180);
     
    JamesThornton and Stephen_O like this.
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I take it you are not using wheel colliders? If you are targetting desktop, consider it. Anyways...

    Code (csharp):
    1.  
    2.  
    3. float maxTurnAngle = 20;
    4.  
    5. void Update()
    6. {
    7.  
    8.    float turnAngle = transform.eulerangles.y + Input.GetAxisRaw("Horizontal");
    9.    if(turnAngle > 180  turnAngle < 360-maxTurnAngle)
    10.      turnAngle = 360-maxTurnAngle;
    11.    else if(turnAngle < 180  turnAngle > maxTurnAngle)
    12.      turnAngle = maxTurnAngle;
    13.  
    14.    transform.rotation = transform.parent.rotation * Quaternion.Euler( 0, turnAngle , 0 );
    15.  
    16. }
    17.  
    18.  

    You can look here for some very helpful information on wheel/car related stuff

    http://www.gotow.net/andrew/blog/?page_id=78
     
  5. rohitbhoir

    rohitbhoir

    Joined:
    Jun 13, 2010
    Posts:
    9
    Hey there, I was working on a car sim proj using a logitech wheel. the thing is that u'll have to divide the rotation of the wheel between (-1 to 1) and (-180 to 180) so the the values of Input.GetAxis("Horizontal") correspond to the angles of the steering wheel.
     
  6. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    I don't think animations are an effective way to realize this simple feature.

    @jeffmorris: this is what I was doing. However, the car moves 90 degress around the x axis but the steering wheel does NOT because you fix it to 0 because of the new Vector3.

    @jlcnz :Thanks for your help. I know that page, it's really helpful, you're right. Nonetheless I couldn't find a solution. I guess you miss my point. I only want the steering wheel inside the cars dashboard to move on ONE axis controlled by the steering wheel (Input.GetAxisRaw...).

    I need to find a way to lock rotations of 2 axis while only the third one is connected to the GetAxis value. You know, what I mean? You can't set a new x axis value like "eulerAngles.x = ..." because this is a return type only.

    Thanks a lot!

    EDIT: This keeps making me crazy!! I just want to rotate the steering wheel insinde my car on one axis...
     
    Last edited: Mar 6, 2012
  7. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    Any further idea please? :-(
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Small mis-understanding in what you were trying to do there...

    Try this...

    Code (csharp):
    1.  
    2.  
    3. float maxTurnAngle = 20;
    4.  
    5. void Update()
    6. {
    7.   transform.Rotate(Input.GetAxisRaw("Horizontal"),0,0);
    8.  
    9.  
    10.    float turnAngle = transform.eulerangles.x;
    11.    if(turnAngle > 180  turnAngle < 360-maxTurnAngle)
    12.      turnAngle = 360-maxTurnAngle;
    13.    else if(turnAngle < 180  turnAngle > maxTurnAngle)
    14.      turnAngle = maxTurnAngle;
    15.  
    16.    transform.rotation = Quaternion.Euler( turnAngle, 0 , 0 );
    17.  
    18. }
    19.  
    20.  
     
  9. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    Unfortunately this doesn't work either.

    With
    Code (csharp):
    1.  transform.rotation = Quaternion.Euler( turnAngle, 0 , 0 );
    You set the y and z angle to a fixed point ( = 0 ). However, the car is moving to the right and to the left, but the steering wheel remains in the same position.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    The sterring wheel remains in the center?

    then try it on the y or the z... its not rocket science
     
  11. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    quick question.. are the drivers hands attached to your steering wheel? sounds funny i know... but depending on whether he's gonna use a driver 3d model or not the solution kind of changes...
     
  12. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    no it doesnt...
     
  13. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    This is not what I was talking about, you miss my point. When my car (as a parent) rotates to the left, rotation values of the car change. But the rotation values (the OTHER TWO I don't want to steer with my axis input) remains in the position 0, because I set them to 0.

    You know?
     
  14. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    isn't it possible to change only ONE axis?

    Things like:
    Code (csharp):
    1. transform.rotation = Quaternion.Euler( turnAngle, 0 , 0 );
    or
    Code (csharp):
    1. ...new Vector3( turnAngle, 0 , 0 );
    definitely DOES NOT work, because axis y and axis z are 0, but the cars rotation changes constantly (as you can imagine ;)).

    I have to make sure, that only one axis will be controlled by the turnAngle variable.
     
  15. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    293
    Desperate bump!
     
  16. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    transform.Rotate(1,0,0);
    transform.localRotation = Quaternion.Euler(15,0,0);
    transform.rotation = Quaternion.Euler(15,0,0);

    either of these will be what youre looking for
     
  17. amanigmtt

    amanigmtt

    Joined:
    Nov 27, 2015
    Posts:
    3
    i have the same problem here i want to let the steering wheel rotate on the 3 axis (x , y ,z) because if i let it rotate on y axe it's going everywhere
     
  18. no00ob

    no00ob

    Joined:
    Dec 13, 2017
    Posts:
    65
    Wait are you using unity 2018? if so this could be a bug because same is happening with me, all other axis work but y breaks the whole thing and it rotates on every axis for some reason.
     
  19. ParadoxSolutions

    ParadoxSolutions

    Joined:
    Jul 27, 2015
    Posts:
    322
    I know this 2012 thread was necro back in May but since people are still looking for a solution here is mine:

    Code (CSharp):
    1.       steeringWheelTransform.localEulerAngles = Vector3.back * Mathf.Clamp((Input.GetAxis("Horizontal") * 100), -maxTurnAngle, maxTurnAngle);
    This will rotate a transform "steeringWheelTransform" left or right along the horizontal input and stop at the float maxTurnAngle (I used 45f) in either direction no matter the orientation/velocity of the steering wheel's parent object.

    If you are looking for a turning steering wheel based on horizontal input, it is just one line of code.
     
    Justice0Juic3 and abenson_fts like this.
  20. kankielegy

    kankielegy

    Joined:
    Jan 14, 2019
    Posts:
    1
    thank you sooooooooooooooooooo much <3
     
    ParadoxSolutions likes this.
  21. LYNXARTS

    LYNXARTS

    Joined:
    Nov 6, 2016
    Posts:
    1
    Even another two years later this saved me some sweet time, thanks!
    Added dampening (steerRotationDamp 0.5) on the rotation for smoother wheelturn;
    Code (CSharp):
    1.        this.transform.localEulerAngles = Vector3.up * Mathf.Clamp((Input.GetAxis("Horizontal") * 100) * steerRotationDamp, -maxTurnAngle, maxTurnAngle);
    2.  
     
  22. frictionvraj123

    frictionvraj123

    Joined:
    Feb 15, 2021
    Posts:
    2
    thanks
     
Thread Status:
Not open for further replies.