Search Unity

Touchscreen Steering Wheel Rotation Example (Mouse Supported)

Discussion in 'Community Learning & Teaching' started by yasirkula, Aug 21, 2013.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi Shane,

    I've tested both scripts without any modifications on my S3 device and they worked perfectly. If you are using the UI code: does the wheel rotate normally when you change its Rotation Z value from Rect Transform component manually?
     
    iamshaneturner likes this.
  2. iamshaneturner

    iamshaneturner

    Joined:
    May 6, 2015
    Posts:
    3
    Hi Yasirkula,

    When I run the app in Unity Remote, the touch works perfectly, but when I build and run, then it goes all weird on me..... It's so strange. I tried a few other methods - they also work perfectly when I run the Remote, but the minute I build and run, then it goes corky.
     
  3. Chzero

    Chzero

    Joined:
    Sep 9, 2015
    Posts:
    9
    Thank you so much! This helped me alot
     
  4. GaZnoDrone

    GaZnoDrone

    Joined:
    Mar 3, 2015
    Posts:
    28
    Hello, I'm using unity 5.2.2 I added your script to my canvas image gameObject. It rotates and rotates back to the inititial rotation but returns 0 wheelangle.Am I doing something wrong I copied the same script that was shown.
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Code (CSharp):
    1. rectT.localEulerAngles = Vector3.back * wheelAngle;
    This line in the UI code uses wheelAngle directly to rotate the steering wheel. So, if your wheel rotates, then wheelAngle must have a value other than 0 (while the wheel is rotated). I honestly don't know why it does not function properly on your project, sorry :)
     
  6. GaZnoDrone

    GaZnoDrone

    Joined:
    Mar 3, 2015
    Posts:
    28

    Dude, your scripts is perfect. it was my error, sorry. Thanks a lot for sharing this script dude.
     
  7. Reloaded23

    Reloaded23

    Joined:
    Jan 4, 2014
    Posts:
    27
    Hi Yasirkula,

    I have got the same problem as faced by iamshaneturner.

    The wheel rotation works perfectly in unity editor but when i build for unity mobile the steering does not work perfectly.
    And yes in the editor when i change the rect rotation z, the steering is rotating perfectly.
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hello Reloaded23,

    Sorry about the late answer. By "does not work perfectly", do you mean that it does not work at all or is it working imperfectly?
     
  9. Reloaded23

    Reloaded23

    Joined:
    Jan 4, 2014
    Posts:
    27
    It is working imperfectly. When i drag diagonally sometimes it steers. But it should work like steering a wheel.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    To be honest, I don't really know why this happens. When I test the code on my S3 phone, it works as expected. Can that line be the reason of the problem, though:

    Code (CSharp):
    1. if( Vector2.Distance( pointerPos, centerPoint ) > 20f )
    If your finger is too close to the center of the wheel (20 pixels), it is ignored. But you can change it to 0 and try again perhaps.
     
  11. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    please help me
    steering wheel is OK. no words to thank you
    But I want to rotate simple 3d object with that Steering wheel(which is working properly)
    I am a maths teacher
    I try to build educational games
    Please some one help me
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hello ranaweera,

    Try this one (RotateMe.cs):

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RotateMe : MonoBehaviour
    4. {
    5.     public SteeringWheel wheelScript;
    6.     public Transform objectToRotate;
    7.    
    8.     void Update()
    9.     {
    10.         objectToRotate.eulerAngles = new Vector3( 0f, wheelScript.GetClampedValue() * 180f, 0f );
    11.     }
    12. }
    This code rotates the "Object To Rotate" around its Y axis using the output of the "Wheel Script". You should assign both variables their values from the Inspector.
     
  13. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    Dear yasirkula
    Great help
    my cube rotating thanks
    Please help the following problem
    I want to rotate around the center
    where should I change in SteeringWheel script
     

    Attached Files:

    • post.png
      post.png
      File size:
      86.3 KB
      Views:
      748
  14. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    Extreamly sorry I forgot to say
    It rotates perfectly around the center
    but the problem comes when I place it on bottom left coner of the screen
     
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Set RectTransform's Pivot to (0.5, 0.5) from the Inspector.
     
    ranaweera likes this.
  16. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    Every thing is ok
    what a great help
    I spent long hours to find these things
    what a great relief to me
    all the credits goes to you
    Wish you all the best dear Yasirkula
    thaks
     
    yasirkula likes this.
  17. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    dear yasirkula
    please again need help
    Now I can turn a little meshine(in my number game) with the steering wheel .
    problem when the steering wheel comes back to zeero position my little meshine also
    spin back .how can I correct this.I tried but did not work .
     
  18. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    Dear yasirkula
    If you are busy just give me an idea for the last issue I posted
    I will try
    Thanks
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    How exactly do you want the meshine to behave?
     
    ranaweera likes this.
  20. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    dear yasikula
    my meshine needs to turned towards prime numbers
    so I want to turn and move alone that direction to collect that numbers
    I can move and collect those numbers but the meshine turns to previous position when the steering wheel is released.
    so now meshine behaves very similar to the steering wheel
    let the steering wheel turns back but need to keep the meshine towards that direction
    (my meshine is avery simple object -no wheel colliders
    thanks
     
  21. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    I got an idea
    I have been trying to turn without tyres
    should I add wheel colliders(at least fron two wheels)
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If your meshine has wheels, then maybe you can use wheel colliders.

    If you want to device to stay still while steering wheel resets its rotation, you can simply set the "Wheel Released Speed" to 0 so that wheel no longer resets to zero rotation.
     
    ranaweera likes this.
  23. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    great I have two options(workig) with your guidence

    sorry for taking your valuable time and disturbing
    so far I have been doing all these things on my own through trial and error method
    This time I got your help
    Thanks Thanks Thanks:)
     
  24. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    some one please help
    think that we access a variable in another scripts .But for some reason if we
    want to reset that value for one object that affects the other object also.
    How can we overcome this issue by restricting to one object.
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Couldn't understand your question well but it is better if you create a new topic for non-related scripting questions :)
     
  26. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    sorry this what happened
    I reset the "Wheel Released Speed" speed to zero
    then the steering wheel did not reset into its previous position
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    That was what I tried to achieve. No reset means no need to make extra adjustments. If you want, you can use the GetAngle function instead of GetClampedValue function and multiply it with a value much smaller than 180f. If you give "Maximum Steering Angle" some very big value, that might work for you.
     
    ranaweera likes this.
  28. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    Ok you nicely put me into the track(nice method of teaching)-thanks dear.
     
  29. gbranca

    gbranca

    Joined:
    Jan 23, 2016
    Posts:
    2
    hi yasirkula, i started using your script for a prototype i am making but for some unknown reason to me i cant manage to get the script to work with the car. i am using the sky car from unity standard assests as a base. using their car controller script. i can get the image to rotate but the car wont steer?
    would greatly appreciate any help you can offer me and thank you sharing the script.

    giuseppe
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hello giuseppe,

    Edit line 22 (or maybe 23, use your instinct =] ) of CarUserControl.cs as following:

    Code (CSharp):
    1. float h = steeringWheel.GetClampedValue();
    Then add a public variable to the same script:

    Code (CSharp):
    1. public SteeringWheel steeringWheel;
    Now, using the Inspector, give the "Steering Wheel" variable of "Car User Control" component of sky car its value and test the game.
     
  31. gbranca

    gbranca

    Joined:
    Jan 23, 2016
    Posts:
    2
    thank you so much for your help. i was stuck for a few days trying to figure it out. works great and thanks again for your support and script :)
     
    yasirkula likes this.
  32. gendgee

    gendgee

    Joined:
    Feb 1, 2013
    Posts:
    4
    Dude tks for sharing this amazing script
     
  33. Hiten2012

    Hiten2012

    Joined:
    Dec 12, 2014
    Posts:
    14
    @yasirkula Thanks a lot, i am using this script. Works perfect in vehicle kind of games.
    Now i am applying changing in that. Steer handle is moving smooth with mouse drag, but I want to make steer handle rigid and slow. Actually i wants to use it as steamer boat steer wheel, which would hard to rotate.
    Can you guide me in script where i need to change.
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hello Hiten2012,

    Add this variable to the script:

    Code (CSharp):
    1. float wheelTargetAngle = 0f;
    Make the following changes:

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.     if( !wheelBeingHeld && !Mathf.Approximately( 0f, wheelAngle ) )
    5.     {
    6.         ...
    7.     }
    8.     else if( wheelBeingHeld )
    9.     {
    10.         wheelAngle = Mathf.Lerp( wheelAngle, wheelTargetAngle, 0.1f );
    11.     }
    12.  
    13.    ...
    14. }
    15.  
    16. public void DragEvent( BaseEventData eventData )
    17. {
    18.     ...
    19.  
    20.     if( Vector2.Distance( pointerPos, centerPoint ) > 20f )
    21.     {
    22.         if( pointerPos.x > centerPoint.x )
    23.             wheelTargetAngle += wheelNewAngle - wheelPrevAngle;
    24.         else
    25.             wheelTargetAngle -= wheelNewAngle - wheelPrevAngle;
    26.     }
    27.     // Make sure wheel angle never exceeds maximumSteeringAngle
    28.     wheelTargetAngle = Mathf.Clamp( wheelTargetAngle, -maximumSteeringAngle, maximumSteeringAngle );
    29.     wheelPrevAngle = wheelNewAngle;
    30. }
    31.  
    Basically, we have added an "else if" statement to Update function and changed "wheelAngle"s to "wheelTargetAngle" inside DragEvent function. Now we have two variables: current rotation of the wheel (wheelAngle) and its target rotation (wheelTargetAngle). You can simply lerp from the former to the latter (which we are doing inside the "else if"). Or you can code your own custom Lerp function for more customization :)

    Hope it works.

    P.S: if you just want to decrease the sensitivity of the wheel, you can get rid of the changes I've suggested above and tweak DragEvent as following:

    Code (CSharp):
    1. public void DragEvent( BaseEventData eventData )
    2. {
    3.     ...
    4.    
    5.     if( Vector2.Distance( pointerPos, centerPoint ) > 20f )
    6.     {
    7.         if( pointerPos.x > centerPoint.x )
    8.             wheelAngle += ( wheelNewAngle - wheelPrevAngle ) * 0.5f;
    9.         else
    10.             wheelAngle -= ( wheelNewAngle - wheelPrevAngle ) * 0.5f;
    11.     }
    12.    
    13.     ...
    14. }
    Adjust 0.5f value to change sensitivity.
     
  35. Hiten2012

    Hiten2012

    Joined:
    Dec 12, 2014
    Posts:
    14
    @yasirkula Thanks a lot for your quick reply.
    second way suggested by you is exactly what i want. decreased sensitivity it works fine. :)
     
    yasirkula likes this.
  36. razcorra

    razcorra

    Joined:
    Jun 21, 2016
    Posts:
    1
    I love you yasirkula I was looking for something like this
     
  37. goutham12

    goutham12

    Joined:
    Jul 14, 2016
    Posts:
    53
    i want use your for my game.i tried your script as it is. its working absolutly.i want to say thanks for that.
    now i want integrate this to my car.can you how can i do it. i need angle and speed. am new that's why am asking.
    just give a idea that how can i do?
    thanks
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You should find the code that receives the input to rotate the car. It should be something like "Input.GetAxis( "Horizontal" )" or "CrossPlatformInput.GetAxis( "Horizontal" )". Afterwards, change that code to something like "GetComponent<SteeringWheel>().GetClampedValue()".
     
  39. goutham12

    goutham12

    Joined:
    Jul 14, 2016
    Posts:
    53
    when i try to copy the whole code into an c# script that was attached to an empty object. it throw some errors .why?
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Name of the C# script should be SteeringWheel. If it already is the case, then what error are you getting from the script?
     
  41. goutham12

    goutham12

    Joined:
    Jul 14, 2016
    Posts:
    53
    check out this this is the error am getting
     

    Attached Files:

  42. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Ah, I see! I have updated the code in the main post. Apparently, it is sufficient to change "events.delegates"s to "events. triggers".
     
  43. goutham12

    goutham12

    Joined:
    Jul 14, 2016
    Posts:
    53
    thanks for your support.it's working absolutely fine. now i have to use get angle method to rotate my front wheels right?
    and for speed?
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    GetAngle returns the angle of the steering wheel itself (I can't think of any uses for that function, actually). You should use GetClampedValue, which returns a float in range [-1,1]. You can, then, multiply that value with a constant (like 45f).

    For accelaration, you should use another asset or create your own script. Mine only handles the simulation of the steering wheel.
     
  45. yoyo696

    yoyo696

    Joined:
    Aug 11, 2015
    Posts:
    14
    thanks so much for the those scripts
    yasirkula how can I make the wheel not to go back to main position when i lift up my finger or mouse.
    please help.
    o_O
     
  46. yoyo696

    yoyo696

    Joined:
    Aug 11, 2015
    Posts:
    14
    OH sorry I found The Solution
    Code (CSharp):
    1.  public void ReleaseEvent( BaseEventData eventData )
    2. {
    3. DragEvent( eventData );
    4.  
    5. //wheelBeingHeld=false;
    6. }
    i just commented wheelBeingHeld=false;
    and it worked.:);):cool::D:rolleyes:o_O
     
    yasirkula likes this.
  47. muhammad_afzaal

    muhammad_afzaal

    Joined:
    Jul 31, 2017
    Posts:
    3
    @yasirkula anyone guide me what to pass to the function PressEvent( event); ????
     
  48. muhammad_afzaal

    muhammad_afzaal

    Joined:
    Jul 31, 2017
    Posts:
    3
    I'm not soo much good in unity.
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You shouldn't call that function manually. You just have to call the GetClampedValue() function to get a value in range [-1,1]. If you need the angle of the steering wheel instead, call GetAngle().
     
  50. junaidhashmi

    junaidhashmi

    Joined:
    May 7, 2017
    Posts:
    4
    @yasirkula thanks for the script...
    working awesome...:D