Advertisement
Muk99

JoystickBehaviour

Jan 7th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5.  
  6. class Boundary {
  7.     public Vector2 min = Vector2.zero;
  8.     public Vector2 max = Vector2.zero;
  9. }
  10. [RequireComponent (typeof (Image))]
  11.  
  12. public class JoystickBehaviour : MonoBehaviour {
  13.  
  14.     [HideInInspector]
  15.     public Vector2 output;                          //Joystick output in [-1, 1]
  16.  
  17.     public Vector2 deadZone = Vector2.zero;         //DeadZone for joystick
  18.     public float maxDistance = 50;                  //MaxDistance from center
  19.     public bool smooth = true;
  20.  
  21.     private Boundary bounds = new Boundary ();      //Boundary for graphics
  22.     private RectTransform joystick;                 //Joystick transform
  23.     private Vector3 defaultPosition;                //Default joystick position
  24.     private int latchedFinger = -1;                 //Latched finger to avoid other fingers to steal joystick
  25.  
  26.     private bool canMove = false;
  27.     private float distanceX = 0;
  28.     private float distanceY = 0;
  29.  
  30.     private float canvasScale;
  31.    
  32.     void Start () {
  33.         // Cache this component at startup instead of looking up every frame
  34.         joystick = GetComponent <RectTransform> ();
  35.  
  36.         //Get canvas scale
  37.         canvasScale = GetComponentInParent <Canvas> ().scaleFactor;
  38.  
  39.         //Default position is its start position
  40.         defaultPosition = joystick.position;
  41.  
  42.         //Build joystick boundary to clamp joystick position
  43.         bounds.min.x = joystick.position.x - maxDistance * canvasScale;
  44.         bounds.max.x = joystick.position.x + maxDistance * canvasScale;
  45.         bounds.min.y = joystick.position.y - maxDistance * canvasScale;
  46.         bounds.max.y = joystick.position.y + maxDistance * canvasScale;
  47.     }
  48.  
  49.     //Reset joystick to its default position
  50.     void ResetPosition () {
  51.         canMove = false;
  52.         latchedFinger = -1;
  53.         joystick.position = defaultPosition;
  54.     }
  55.  
  56.     void Update () {
  57.         //Graphics size /2 to calculate distance between input position and graphics position
  58.         float sizeX = joystick.sizeDelta.x/2;
  59.         float sizeY = joystick.sizeDelta.y/2;
  60.  
  61.         #if UNITY_EDITOR
  62.  
  63.         //Distance between joystick graphics snd mouse position
  64.         distanceX = defaultPosition.x - Input.mousePosition.x;
  65.         distanceY = defaultPosition.y - Input.mousePosition.y;
  66.  
  67.         //Calculate if mouse position is inside joystick graphics
  68.         bool containsMousePosition = distanceX >= -sizeX &&
  69.                                      distanceX <=  sizeX &&
  70.                                      distanceY >= -sizeY &&
  71.                                      distanceY <=  sizeY ;
  72.  
  73.         //Allow mouse move if it's inside joystick graphics
  74.         if (Input.GetMouseButtonDown (0) && containsMousePosition)
  75.             canMove = true;
  76.  
  77.         // Change the location of the joystick graphic to match where the mouse is and clamp its position
  78.         if (Input.GetMouseButton (0) && canMove){
  79.             Vector3 pos = joystick.position;
  80.             pos.x = Mathf.Clamp (Input.mousePosition.x, bounds.min.x, bounds.max.x);
  81.             pos.y = Mathf.Clamp (Input.mousePosition.y, bounds.min.y, bounds.max.y);
  82.             joystick.position = pos;
  83.         }
  84.  
  85.         // Reset position if click ended
  86.         if (Input.GetMouseButtonUp (0))
  87.             ResetPosition ();
  88.  
  89.         #else
  90.  
  91.         foreach (Touch touch in Input.touches){
  92.             //Distance between joystick graphics snd touch position
  93.             distanceX = defaultPosition.x - touch.position.x;
  94.             distanceY = defaultPosition.y - touch.position.y;
  95.            
  96.             //Calculate if touch position is inside joystick graphics
  97.             bool containsTouchPosition = distanceX >= -sizeX &&
  98.                                          distanceX <=  sizeX &&
  99.                                          distanceY >= -sizeY &&
  100.                                          distanceY <=  sizeY ;
  101.  
  102.             //Allow touch move if it's inside joystick graphics & get latched finger id
  103.             if (containsTouchPosition && touch.phase == TouchPhase.Began){
  104.                 latchedFinger = touch.fingerId;
  105.                 canMove = true;
  106.             }  
  107.  
  108.             // Change the location of the joystick graphic to match where the mouse is and clamp its position
  109.             if (canMove && touch.fingerId == latchedFinger){
  110.                 Vector3 pos = joystick.position;
  111.                 pos.x = Mathf.Clamp (touch.position.x, bounds.min.x, bounds.max.x);
  112.                 pos.y = Mathf.Clamp (touch.position.y, bounds.min.y, bounds.max.y);
  113.                 joystick.position = pos;
  114.             }
  115.  
  116.             // Reset position if touch ended
  117.             if (touch.fingerId == latchedFinger && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
  118.                 ResetPosition ();
  119.         }
  120.  
  121.         #endif
  122.  
  123.         // Get a value between -1 and 1 based on the joystick graphic location
  124.         output.x = (joystick.position.x - defaultPosition.x) / (maxDistance * canvasScale);
  125.         output.y = (joystick.position.y - defaultPosition.y) / (maxDistance * canvasScale);
  126.  
  127.         // Adjust for dead zone
  128.         var absoluteX = Mathf.Abs( output.x );
  129.         var absoluteY = Mathf.Abs( output.y );
  130.        
  131.         if ( absoluteX < deadZone.x )
  132.             output.x = 0;
  133.        
  134.         if ( absoluteY < deadZone.y )
  135.             output.y = 0;
  136.  
  137.         //Smooth value, making it less sensible near center
  138.         if ( smooth ) {
  139.             output.y = output.y * output.y;
  140.             output.x = output.x * output.x;
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement