Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- class Boundary {
- public Vector2 min = Vector2.zero;
- public Vector2 max = Vector2.zero;
- }
- [RequireComponent (typeof (Image))]
- public class JoystickBehaviour : MonoBehaviour {
- [HideInInspector]
- public Vector2 output; //Joystick output in [-1, 1]
- public Vector2 deadZone = Vector2.zero; //DeadZone for joystick
- public float maxDistance = 50; //MaxDistance from center
- public bool smooth = true;
- private Boundary bounds = new Boundary (); //Boundary for graphics
- private RectTransform joystick; //Joystick transform
- private Vector3 defaultPosition; //Default joystick position
- private int latchedFinger = -1; //Latched finger to avoid other fingers to steal joystick
- private bool canMove = false;
- private float distanceX = 0;
- private float distanceY = 0;
- private float canvasScale;
- void Start () {
- // Cache this component at startup instead of looking up every frame
- joystick = GetComponent <RectTransform> ();
- //Get canvas scale
- canvasScale = GetComponentInParent <Canvas> ().scaleFactor;
- //Default position is its start position
- defaultPosition = joystick.position;
- //Build joystick boundary to clamp joystick position
- bounds.min.x = joystick.position.x - maxDistance * canvasScale;
- bounds.max.x = joystick.position.x + maxDistance * canvasScale;
- bounds.min.y = joystick.position.y - maxDistance * canvasScale;
- bounds.max.y = joystick.position.y + maxDistance * canvasScale;
- }
- //Reset joystick to its default position
- void ResetPosition () {
- canMove = false;
- latchedFinger = -1;
- joystick.position = defaultPosition;
- }
- void Update () {
- //Graphics size /2 to calculate distance between input position and graphics position
- float sizeX = joystick.sizeDelta.x/2;
- float sizeY = joystick.sizeDelta.y/2;
- #if UNITY_EDITOR
- //Distance between joystick graphics snd mouse position
- distanceX = defaultPosition.x - Input.mousePosition.x;
- distanceY = defaultPosition.y - Input.mousePosition.y;
- //Calculate if mouse position is inside joystick graphics
- bool containsMousePosition = distanceX >= -sizeX &&
- distanceX <= sizeX &&
- distanceY >= -sizeY &&
- distanceY <= sizeY ;
- //Allow mouse move if it's inside joystick graphics
- if (Input.GetMouseButtonDown (0) && containsMousePosition)
- canMove = true;
- // Change the location of the joystick graphic to match where the mouse is and clamp its position
- if (Input.GetMouseButton (0) && canMove){
- Vector3 pos = joystick.position;
- pos.x = Mathf.Clamp (Input.mousePosition.x, bounds.min.x, bounds.max.x);
- pos.y = Mathf.Clamp (Input.mousePosition.y, bounds.min.y, bounds.max.y);
- joystick.position = pos;
- }
- // Reset position if click ended
- if (Input.GetMouseButtonUp (0))
- ResetPosition ();
- #else
- foreach (Touch touch in Input.touches){
- //Distance between joystick graphics snd touch position
- distanceX = defaultPosition.x - touch.position.x;
- distanceY = defaultPosition.y - touch.position.y;
- //Calculate if touch position is inside joystick graphics
- bool containsTouchPosition = distanceX >= -sizeX &&
- distanceX <= sizeX &&
- distanceY >= -sizeY &&
- distanceY <= sizeY ;
- //Allow touch move if it's inside joystick graphics & get latched finger id
- if (containsTouchPosition && touch.phase == TouchPhase.Began){
- latchedFinger = touch.fingerId;
- canMove = true;
- }
- // Change the location of the joystick graphic to match where the mouse is and clamp its position
- if (canMove && touch.fingerId == latchedFinger){
- Vector3 pos = joystick.position;
- pos.x = Mathf.Clamp (touch.position.x, bounds.min.x, bounds.max.x);
- pos.y = Mathf.Clamp (touch.position.y, bounds.min.y, bounds.max.y);
- joystick.position = pos;
- }
- // Reset position if touch ended
- if (touch.fingerId == latchedFinger && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
- ResetPosition ();
- }
- #endif
- // Get a value between -1 and 1 based on the joystick graphic location
- output.x = (joystick.position.x - defaultPosition.x) / (maxDistance * canvasScale);
- output.y = (joystick.position.y - defaultPosition.y) / (maxDistance * canvasScale);
- // Adjust for dead zone
- var absoluteX = Mathf.Abs( output.x );
- var absoluteY = Mathf.Abs( output.y );
- if ( absoluteX < deadZone.x )
- output.x = 0;
- if ( absoluteY < deadZone.y )
- output.y = 0;
- //Smooth value, making it less sensible near center
- if ( smooth ) {
- output.y = output.y * output.y;
- output.x = output.x * output.x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement