/// <summary> /// Function for joystick tweaking (moving with the finger) /// The values of the Axis are also calculated here /// </summary> /// <param name="touchPosition">Current touch position in screen cooridnates (pixels) /// It's recalculated in units so it's resolution-independent</param> protected override void TweakControl(Vector2 touchPosition) { // First, let's find our current touch position in world space Vector3 worldTouchPosition = ParentCamera.ScreenToWorldPoint(touchPosition); // Now we need to find a directional vector from the center of the joystick // to the touch position Vector3 differenceVector = (worldTouchPosition - _baseTransform.position); // If we're out of the drag range if (differenceVector.sqrMagnitude > DragRadius * DragRadius) { // Normalize this directional vector differenceVector.Normalize(); // And place the stick to it's extremum position _stickTransform.position = _baseTransform.position + differenceVector * DragRadius; } else { // If we're inside the drag range, just place it under the finger _stickTransform.position = worldTouchPosition; } // Store calculated axis values to our private variable CurrentAxisValues = differenceVector; // We also fire our event if there are subscribers OnControllerMoved(differenceVector); }
/// <summary> /// Snap the joystick under the finger if it's expected /// </summary> /// <param name="touch">Current touch position in screen pixels /// It converts pixels to world space coordinates so it's resolution independent</param> protected virtual void PlaceJoystickBaseUnderTheFinger(Touch touch) { if (!_snapsToFinger) { return; } _stickTransform.position = _baseTransform.position = ParentCamera.ScreenToWorldPoint(touch.position); }
/// <summary> /// Automatically called by TweakIfNeeded /// </summary> /// <param name="touchPosition">Touch position in screen pixels</param> protected override void TweakControl(Vector2 touchPosition) { Vector3 worldPosition = ParentCamera.ScreenToWorldPoint(touchPosition); Vector3 difference = worldPosition - PreviousPosition; if (IsAlwaysNormalized) { difference.Normalize(); } CurrentAxisValues = difference; OnControllerMoved(difference); PreviousPosition = worldPosition; }
/// <summary> /// Good old Update method where all the magic happens /// </summary> protected virtual void Update() { // If we tweaked, we return and don't check for other touches if (TweakIfNeeded()) { return; } // If we didn't tweak, we try to capture any touch Touch currentTouch; if (!IsTouchCaptured(out currentTouch)) { return; } // Setting our initial "previous" position PreviousPosition = ParentCamera.ScreenToWorldPoint(currentTouch.position); }
/// <summary> /// Utility method, chechks whether the touch is inside the touch zone (green rect) /// </summary> /// <param name="touchPosition">Current touch position in screen pixels /// It converts these pixels to units, so it's totally resolution-independent /// </param> /// <returns>Whether it's inside of the touch zone</returns> private bool IsTouchInZone(Vector2 touchPosition) { return(CalculatedTouchZone.Contains(ParentCamera.ScreenToWorldPoint(touchPosition), false)); }
protected override void MoreUpdateLogic(Touch capturedTouch) { PreviousPosition = ParentCamera.ScreenToWorldPoint(capturedTouch.position); }