Advertisement
rhose87

iPhoneAnalogStick

Feb 6th, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var touchZoneWidth : int = 60;
  2. var touchZoneHeight : int = 60;
  3. private var leftStickDown : boolean = false;
  4. private var rightStickDown : boolean = false;
  5. private var leftStickBounds : Rect;
  6. private var leftStickCenter : Vector2;
  7. private var leftStickTouchId : int = -1;
  8. private var rightStickTouchId : int = -1;
  9.  
  10. var fpsWalker : FPSWalkeriPhone;
  11. var look1 : MouseLookiPhone;
  12. var look2 : MouseLookiPhone;
  13.  
  14. function Start ()
  15. {
  16.     // let's be in widescreen mode
  17.     Screen.orientation = ScreenOrientation.LandscapeLeft;
  18.    
  19.     // set the bounds at start, so we can see these in the inspector
  20.     leftStickBounds = Rect(0, 0, touchZoneWidth, touchZoneHeight);
  21.     leftStickCenter = Vector2 (touchZoneWidth * 0.5, touchZoneHeight * 0.5);
  22. }
  23.  
  24. function Update (){
  25.     // pull information out of each touch event
  26.     // empty out the touch event array, set it to the length of the number of touches
  27.     var count = Input.touchCount;
  28.     for (var i : int = 0; i < count; i++)
  29.     {
  30.         // cram the touch event into the array
  31.         var touch : Touch = Input.GetTouch(i);
  32.        
  33.         // check the phase FIRST
  34.         if (touch.phase == TouchPhase.Began)
  35.         {
  36.             // check the actual point of the touch
  37.             // if it's inside either button, store the position as the center
  38.            
  39.            
  40.            
  41.             // only perform this check if the left stick isn't already pressed
  42.             if (!leftStickDown)
  43.             {
  44.                 // check if we pressed down on the left stick
  45.                 leftStickDown = IsThumbInsideBounds(touch.position, leftStickBounds);
  46.                 // if we did, don't even check the right stick
  47.                 if (leftStickDown)
  48.                 {
  49.                     leftStickTouchId = touch.fingerId;
  50.                     continue;
  51.                 }
  52.             }
  53.            
  54.             // only perform this check if the right stick isn't already pressed
  55.             if (!rightStickDown)
  56.             {
  57.                 // we consider whole screen except the left-stick area as a right-stick
  58.                 rightStickDown = !IsThumbInsideBounds(touch.position, leftStickBounds);
  59.                 if (rightStickDown)
  60.                 {
  61.                     rightStickTouchId = touch.fingerId;
  62.                     continue;
  63.                 }
  64.             }
  65.         }
  66.         else if (touch.phase == TouchPhase.Moved)
  67.         {
  68.             // figure out if we're looking at the left thumb
  69.             if (leftStickDown && leftStickTouchId == touch.fingerId)
  70.             {
  71.                 var lDiff : Vector2 = touch.position - leftStickCenter;
  72.                 var lDiff3 : Vector3 = Vector3 (lDiff.x, 0, lDiff.y);
  73.                
  74.                 // make sure direction is inside unit sphere
  75.                 if (lDiff3.sqrMagnitude > 1)
  76.                     lDiff3.Normalize();
  77.                 fpsWalker.SetMoveDirection (lDiff3);
  78.             }
  79.            
  80.             // figure out if we're looking at the right thumb
  81.             if (rightStickDown && rightStickTouchId == touch.fingerId)
  82.             {
  83.                 //look1.SetInput (touch.deltaPosition);
  84.                 //look2.SetInput (touch.deltaPosition);
  85.             }
  86.         }
  87.         else if (touch.phase == TouchPhase.Stationary)
  88.         {
  89.             // stop the right stick from rotating if it's stationary
  90.             if (rightStickDown && rightStickTouchId == touch.fingerId)
  91.             {
  92.                 //look1.SetInput (Vector2.zero);
  93.                 //look2.SetInput (Vector2.zero);
  94.             }
  95.         }
  96.         else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  97.         {
  98.             // release left stick
  99.             if (leftStickDown && leftStickTouchId == touch.fingerId)
  100.             {
  101.                 leftStickDown = false;
  102.                 fpsWalker.SetMoveDirection (Vector3.zero);
  103.                 leftStickTouchId = -1;
  104.             }
  105.            
  106.             // release 2nd finger
  107.             if (rightStickDown && rightStickTouchId == touch.fingerId)
  108.             {
  109.                 rightStickDown = false;
  110.                 //look1.SetInput (Vector2.zero);
  111.                 //look2.SetInput (Vector2.zero);
  112.                 rightStickTouchId = -1;
  113.             }
  114.         }
  115.     }
  116.    
  117.    
  118.     //look1.CustomUpdate();
  119.     //look2.CustomUpdate();
  120.     fpsWalker.CustomUpdate();
  121. }
  122.  
  123.  
  124. function IsThumbInsideBounds (touchPosition : Vector2, analogBounds : Rect)
  125. {
  126.     return (analogBounds.Contains (touchPosition));
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement