Example #1
0
 // Update is called once per frame
 void Update()
 {
     foreach (Touch thisTouch in Input.touches)
     {
         if (thisTouch.phase == TouchPhase.Began)
         {
             // we must have a new baby finger!
             FingerObj newFinger = Instantiate(fingerPrefab, TouchHelper.GetTouchWorldPosition(thisTouch), Quaternion.identity);
             // Instantiate all member variables the ugly way!
             newFinger.fingerID           = thisTouch.fingerId;
             newFinger.transform.position = TouchHelper.GetTouchWorldPosition(thisTouch);
             newFinger.touchLastFrame     = newFinger.touch = newFinger.originTouch = thisTouch;
             currentFingers.Add(newFinger);
             if (OnNewFinger != null)
             {
                 OnNewFinger(newFinger);
             }
         }
     }
     if (Input.mousePresent)
     {
         if (Input.GetMouseButtonDown(0))
         {
             // Debug.Log( "we got a mouse click");
             // Create a new baby mouse finger 🐁
             Touch     thisTouch = TouchHelper.GetFakeMouseTouch();
             FingerObj newFinger = Instantiate(fingerPrefab, TouchHelper.GetTouchWorldPosition(thisTouch), Quaternion.identity);
             // Instantiate all member variables the ugly way!
             newFinger.fingerID           = thisTouch.fingerId;
             newFinger.transform.position = TouchHelper.GetTouchWorldPosition(thisTouch);
             newFinger.touchLastFrame     = newFinger.touch = newFinger.originTouch = thisTouch;
             currentFingers.Add(newFinger);
             if (OnNewFinger != null)
             {
                 OnNewFinger(newFinger);
             }
         }
     }
     foreach (FingerObj thisFinger  in currentFingers)
     {
         thisFinger.UpdateFinger();
         if (FingersDoneUpdating != null)
         {
             FingersDoneUpdating();
         }
     }
 }
Example #2
0
    /// <summary>
    /// Gets the first object in the direction of wherever the user tapped.
    /// </summary>
    /// <returns>Returns the raycast collision itself so that the position of contact can be preserved. Returns an empty `RaycastHit2D` if the object hit does not contain a `<see cref="StickInteractable" />`. You can evaluate a `RaycastHit2D` as a boolean to determine if this function succeeded or failed.</returns>
    RaycastHit2D GetFirstObjHitByStick(Touch finger)
    {
        Vector2      tapLocation = TouchHelper.GetTouchWorldPosition(finger);
        float        radius      = finger.radius + expandTouchRadiusBy;
        Vector2      direction   = tapLocation - (Vector2)transform.position;
        RaycastHit2D hit         = Physics2D.CircleCast(tapLocation, radius, direction.normalized, Mathf.Max(maxTapDistance, direction.magnitude));

        // just play the hits 💿 ayy lmao
        if (hit && hit.collider.GetComponent <StickInteractable>() != null)
        {
            return(hit);
        }
        else
        {
            return(new RaycastHit2D());
        }
    }
    // Use this for initialization

    public void UpdateFinger()
    {
        if (!calledThisFrameYet)
        {
            gameObject.name    = "Finger - " + fingerID;
            calledThisFrameYet = true;
            touchLastFrame     = touch;
            touch = TouchHelper.GetTouchByFingerID(fingerID);
            Vector3 newPos = TouchHelper.GetTouchWorldPosition(touch);
            newPos.z           = 0f;
            transform.position = newPos;

            // determine the 🗾 state
            if (currentState == TouchState.Tap)
            {
                // are we a drag 👑 yet?
                Vector2 distance = touch.position - originTouch.position;
                if (distance.magnitude > TouchManager.inst.dragDetectionDistPixels)
                {
                    currentState = TouchState.Dragging;
                    if (OnStatusChange != null)
                    {
                        OnStatusChange(this);
                    }
                }
            }

            // or are we to 💀 die
            if (IsFingerLeaving(this))
            {
                currentState = TouchState.Ending;
                if (OnStatusChange != null)
                {
                    OnStatusChange(this);
                }
                Destroy(this.gameObject);
            }
            if (OnFingerUpdated != null)
            {
                OnFingerUpdated(this);
            }
        }
    }