Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mobile touch steering wheel?

Discussion in 'Scripting' started by nbg_yalta, Dec 20, 2014.

  1. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Hi, I'm trying to make a screen touch steering wheel with new Unity UI
    I've found this script, but it was done using old unity GUI
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. public var maximumAngle : float = 500f; // Maximum angle the steering wheel can rotate
    5. public var wheelSize : float = 256f; // Wheel's width (and height) as pixel
    6. public var deltaPivot : Vector2 = Vector2.zero; // If wheel not rotates around its center, this variable allows tweaking the pivot point
    7. public var wheelFreeSpeed : float = 200f; // Degrees per second the wheel rotates when released
    8. public var wheelTexture : Texture2D; // Wheel texture
    9.  
    10. private var wheelAngle : float; // Wheel's angle in degrees
    11.  
    12. private var wheelBeingHeld : boolean; // Whether or not the steering wheel is being held
    13. private var wheelPosition : Rect; // Wheel's position on screen
    14. private var wheelCenter : Vector2; // Wheel's center on screen coordinates (not Rect coordinates)
    15. private var wheelTempAngle : float; // A necessary variable
    16.  
    17. function Start()
    18. {
    19.     // Initialize variables and calculate wheel's position on screen
    20.     wheelBeingHeld = false;
    21.     wheelPosition = new Rect( Screen.width - wheelSize - 75, Screen.height - wheelSize - 75, wheelSize, wheelSize );
    22.     wheelCenter = new Vector2( wheelPosition.x + wheelPosition.width * 0.5f, Screen.height - wheelPosition.y - wheelPosition.height * 0.5f );
    23.     wheelAngle = 0f;
    24. }
    25.  
    26. // Returns the angle of the steering wheel in degrees. Can be used to rotate a car etc.
    27. public function GetAngle()
    28. {
    29.     return wheelAngle;
    30. }
    31.  
    32. function Update()
    33. {
    34.     // Show the rotation on console
    35.     print( wheelAngle );
    36.    
    37.     // If the wheel is currently being held
    38.     if( wheelBeingHeld )
    39.     {
    40.         var mousePosition : Vector2;
    41.  
    42.         // Find the mouse position on screen
    43.         mousePosition = Input.mousePosition;
    44.            
    45.         var wheelNewAngle : float = Vector2.Angle( Vector2.up, mousePosition - wheelCenter );
    46.        
    47.         // If mouse is very close to the steering wheel's center, do nothing
    48.         if( Vector2.Distance( mousePosition, wheelCenter ) > 20f )
    49.         {
    50.             if( mousePosition.x > wheelCenter.x )
    51.                 wheelAngle -= wheelNewAngle - wheelTempAngle;
    52.             else
    53.                 wheelAngle += wheelNewAngle - wheelTempAngle;
    54.         }
    55.        
    56.         // Make sure that the wheelAngle does not exceed the maximumAngle
    57.         if( wheelAngle > maximumAngle )
    58.             wheelAngle = maximumAngle;
    59.         else if( wheelAngle < -maximumAngle )
    60.             wheelAngle = -maximumAngle;
    61.        
    62.         wheelTempAngle = wheelNewAngle;
    63.        
    64.         // If user releases the mouse, release the wheel
    65.         if( Input.GetMouseButtonUp( 0 ) )
    66.             wheelBeingHeld = false;
    67.     }
    68.     else // If wheel is not being held
    69.     {
    70.         // If user clicks on the wheel, update the status
    71.         if( Input.GetMouseButtonDown( 0 )  wheelPosition.Contains( new Vector2( Input.mousePosition.x, Screen.height - Input.mousePosition.y ) ) )
    72.         {
    73.             wheelBeingHeld = true;
    74.             wheelTempAngle = Vector2.Angle( Vector2.up, Input.mousePosition - wheelCenter );
    75.         }
    76.        
    77.         // If the wheel is rotated and not being held, rotate it to its default angle (zero)
    78.         if( !Mathf.Approximately( 0f, wheelAngle ) )
    79.         {
    80.             var deltaAngle : float = wheelFreeSpeed * Time.deltaTime;
    81.            
    82.             if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )
    83.             {
    84.                 wheelAngle = 0f;
    85.                 return;
    86.             }
    87.            
    88.             if( wheelAngle > 0f )
    89.                 wheelAngle -= deltaAngle;
    90.             else
    91.                 wheelAngle += deltaAngle;
    92.         }
    93.     }
    94. }
    95.  
    96. // Draw the steering wheel on screen
    97. function OnGUI()
    98. {
    99.     // Uncomment the line below to see the bounds of the wheel
    100.     // GUI.Box( wheelPosition, "" );
    101.    
    102.     var theMatrix : Matrix4x4 = GUI.matrix;
    103.     GUIUtility.RotateAroundPivot( -wheelAngle, wheelPosition.center + deltaPivot );
    104.     GUI.DrawTexture( wheelPosition, wheelTexture );
    105.     GUI.matrix = theMatrix;
    106. }
    107.  
    Can some one help me with this? I can't even find a center of my UI Image to get started...