playMaker

Author Topic: Rotating a GUI Steering wheel on touch/drag?  (Read 6880 times)

derkoi

  • Full Member
  • ***
  • Posts: 187
Rotating a GUI Steering wheel on touch/drag?
« on: September 13, 2013, 07:07:00 AM »
I'm making a mobile racing game and one of the control methods players expect is an on screen steering wheel they can drag.

I use NGUI but I'm not sure how to go about this, any suggestions?

Thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Rotating a GUI Steering wheel on touch/drag?
« Reply #1 on: September 13, 2013, 08:11:40 AM »
Hi,

 I guess you simply need to have your gameobject looking at the mouse or touch to rotate accordingly, is it what you are after? or the problem is elsewhere?

bye,

 Jean

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: Rotating a GUI Steering wheel on touch/drag?
« Reply #2 on: September 13, 2013, 08:24:18 AM »
yeah, that would work i think. I found some code but I don't understand it.

Code: [Select]
    #pragma strict
     
    public var maximumAngle : float = 500f; // Maximum angle the steering wheel can rotate
    public var wheelSize : float = 256f; // Wheel's width (and height) as pixel
    public var deltaPivot : Vector2 = Vector2.zero; // If wheel not rotates around its center, this variable allows tweaking the pivot point
    public var wheelFreeSpeed : float = 200f; // Degrees per second the wheel rotates when released
    public var wheelTexture : Texture2D; // Wheel texture
     
    private var wheelAngle : float; // Wheel's angle in degrees
     
    private var wheelBeingHeld : boolean; // Whether or not the steering wheel is being held
    private var wheelPosition : Rect; // Wheel's position on screen
    private var wheelCenter : Vector2; // Wheel's center on screen coordinates (not Rect coordinates)
    private var wheelTempAngle : float; // A necessary variable
    private var touchId : int = -1; // The finger holding the wheel
     
    function Start()
    {
        // Initialize variables and calculate wheel's position on screen
        wheelBeingHeld = false;
        wheelPosition = new Rect( Screen.width - wheelSize - 75, Screen.height - wheelSize - 75, wheelSize, wheelSize );
        wheelCenter = new Vector2( wheelPosition.x + wheelPosition.width * 0.5f, Screen.height - wheelPosition.y - wheelPosition.height * 0.5f );
        wheelAngle = 0f;
    }
     
    // Returns the angle of the steering wheel in degrees. Can be used to rotate a car etc.
    public function GetAngle()
    {
        return wheelAngle;
    }
     
    function Update()
    {
        // Show the rotation on console
        print( wheelAngle );
       
        // If the wheel is currently being held
        if( wheelBeingHeld )
        {
            var touchPosition : Vector2;
     
            // Find the finger position on screen
            for( var t : Touch in Input.touches )
            {
                if( t.fingerId == touchId )
                {
                    touchPosition = t.position;
                   
                    // If finger exists no more, release the wheel
                    if( t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled )
                    {
                        wheelBeingHeld = false;
                    }
                }
            }
               
            var wheelNewAngle : float = Vector2.Angle( Vector2.up, touchPosition - wheelCenter );
           
            // If touch is very close to the steering wheel's center, do nothing
            if( Vector2.Distance( touchPosition, wheelCenter ) > 20f )
            {
                if( touchPosition.x > wheelCenter.x )
                    wheelAngle -= wheelNewAngle - wheelTempAngle;
                else
                    wheelAngle += wheelNewAngle - wheelTempAngle;
            }
           
            // Make sure that the wheelAngle does not exceed the maximumAngle
            if( wheelAngle > maximumAngle )
                wheelAngle = maximumAngle;
            else if( wheelAngle < -maximumAngle )
                wheelAngle = -maximumAngle;
           
            wheelTempAngle = wheelNewAngle;
        }
        else // If wheel is not being held
        {
            // If a finger touches the wheel, update the status
            for( var t : Touch in Input.touches )
            {
                if( t.phase == TouchPhase.Began )
                {
                    if( wheelPosition.Contains( new Vector2( t.position.x, Screen.height - t.position.y ) ) )
                    {
                        wheelBeingHeld = true;
                        wheelTempAngle = Vector2.Angle( Vector2.up, t.position - wheelCenter );
                        touchId = t.fingerId;
                    }
                }
            }
           
            // If the wheel is rotated and not being held, rotate it to its default angle (zero)
            if( !Mathf.Approximately( 0f, wheelAngle ) )
            {
                var deltaAngle : float = wheelFreeSpeed * Time.deltaTime;
               
                if( Mathf.Abs( deltaAngle ) > Mathf.Abs( wheelAngle ) )
                {
                    wheelAngle = 0f;
                    return;
                }
               
                if( wheelAngle > 0f )
                    wheelAngle -= deltaAngle;
                else
                    wheelAngle += deltaAngle;
            }
        }
    }
     
    // Draw the steering wheel on screen
    function OnGUI()
    {
        // Uncomment the line below to see the bounds of the wheel
        // GUI.Box( wheelPosition, "" );
       
        var theMatrix : Matrix4x4 = GUI.matrix;
        GUIUtility.RotateAroundPivot( -wheelAngle, wheelPosition.center + deltaPivot );
        GUI.DrawTexture( wheelPosition, wheelTexture );
        GUI.matrix = theMatrix;
    }