IEnumerator AnimateAlpha(SwipeableObject line, bool inMiddle, float duration) { Color color = line.GetColor(); float alpha = color.a; float targetAlpha = inMiddle ? 1f : disabledAlpha; int steps = Mathf.Max(1, (int)(duration / 0.01f)); for (int i = 0; i < steps; i++) { float t = (float)(i + 1) / (float)(steps); line.SetTransparency(alpha * (1 - t) + targetAlpha * t); if (i < steps - 1) { yield return(new WaitForSeconds(0.01f)); } } line.IsAnimating = false; line.OnSwipeAnimCompleted.Invoke(); // Add a short delay after alpha animation completes yield return(new WaitForSeconds(delayBeforeEnablingSwipe)); // Enable the line if it is in middle; otherwise disable it. line.IsAvailableForSwipe = inMiddle; }
IEnumerator AnimateYThenResetX(SwipeableObject line, Vector3 destination) { // First animate to destination Vector3 pos = line.transform.position; int steps = Mathf.Max(1, (int)(vertAnimDuration / 0.01f)); for (int i = 0; i < steps; i++) { line.transform.position = Vector3.Lerp(pos, destination, (float)(i + 1) / (float)(steps)); if (i < steps - 1) { yield return(new WaitForSeconds(0.01f)); } } // Then reset X position as necessary if (Mathf.Abs(line.transform.position.x - defaultPosition.x) > EPS) // need to reset { destination = line.transform.position; destination.x = defaultPosition.x; // reset x yield return(new WaitForSeconds(0.5f)); StartCoroutine(AnimateToDestination(line, destination, horiAnimDuration)); } }
/////////////////////////////////////////////////////////////////////////////////////// // helper functions /// <summary> /// Helper function to dertermine if hand is in position to start swiping. /// </summary> bool CanStartSwiping(SwipeableObject obj) { if (obj != null) { return(handOnscreen && InRange() && InBounds(obj)); } return(false); }
void Start() { cam = Camera.main; cardImage.sprite = card.cardImage; cardName.text = card.cardName; cardDescription.text = card.cardDescription; swipeRef = FindObjectOfType <SwipeScript>(); swipeObj = GetComponent <SwipeableObject> (); myTransform = GetComponent <Transform>(); }
IEnumerator AnimateToDestination(SwipeableObject line, Vector3 destination, float duration) { Vector3 pos = transform.position; int steps = Mathf.Max(1, (int)(duration / 0.01f)); for (int i = 0; i < steps; i++) { line.transform.position = Vector3.Lerp(pos, destination, (float)(i + 1) / (float)(steps)); if (i < steps - 1) { yield return(new WaitForSeconds(0.01f)); } } }
IEnumerator DoAnimationVertical(SwipeableObject line, Vector3 destination) { if (line == null || line.IsAnimating) { yield break; } line.IsAnimating = true; line.OnSwipeAnimStarted.Invoke(); StartCoroutine(AnimateYThenResetX(line, destination)); yield return(new WaitForSeconds(vertAnimDuration)); bool inMiddle = (Mathf.Abs(destination.y - (ymax + ymin) / 2) < EPS); StartCoroutine(AnimateAlpha(line, inMiddle, alphaFadeDuration)); }
bool InBounds(SwipeableObject obj) { RectRelativeToRect_X xdim; RectRelativeToRect_Y ydim; if (HandOverlapsCollider(obj.boundingBox, out xdim, out ydim)) { return(true); } else { if (xdim == RectRelativeToRect_X.Right || xdim == RectRelativeToRect_X.Left) { if (moveDir == MoveDir.Right || moveDir == MoveDir.Left) // move horizontally { return(ydim == RectRelativeToRect_Y.Overlap); } else { return(false); } } if (ydim == RectRelativeToRect_Y.Above || ydim == RectRelativeToRect_Y.Below) { if (moveDir == MoveDir.Up || moveDir == MoveDir.Down) // move vertically { return(xdim == RectRelativeToRect_X.Overlap); } else { return(false); } } } return(false); }
void UpdateSwipeStates() { if (swipeState == SwipeState.Idle || swipeState == SwipeState.Activated) { SwipeableObject nextActiveObject = null; // Iterate all swipeable objects foreach (SwipeableObject obj in SwipeableObject.instances) { // find one that is currently available and in reach. if (obj.IsAvailableForSwipe && !obj.IsAnimating && CanStartSwiping(obj)) { nextActiveObject = obj; break; } } if (activeObject != nextActiveObject) { if (nextActiveObject != null) { swipeState = SwipeState.Activated; Activate(); } else { swipeState = SwipeState.Idle; Deactivate(); } activeObject = nextActiveObject; } } if (swipeState != SwipeState.Idle) { UpdateSwipeData(); // Calculate hand move speed float speed = (durationInOneDirection > 0) ? (distInOneDirection / durationInOneDirection) : 0; // It is a "fast" swipe, if (1) the speed of the hand in the swiping direction is greater than speedThresh (m/s), // AND (2) the distance the hand moved continously in the swiping direction is greater than distThresh (m). bool fastSwipe = (speed > speedThresh && distInOneDirection > distThresh); if (fastSwipe) { if (activeObject != null) { activeObject.OnSwipe.Invoke(moveDir); } swipeState = SwipeState.Swiping; OnSwipeSucceeded.Invoke(); } else if (speed > 0) // user is moving "slowly" { Vector3 objPos = activeObject.transform.position; Vector3 handPos = this.transform.position; // scale that projects hand dimensions to object depth float s = objPos.z / handPos.z; if (activeObject != null) { activeObject.OnMove.Invoke(distCurFrame * s, moveDir); } swipeState = SwipeState.Moving; } else { swipeState = SwipeState.Activated; } } // Treat SwipeState.Moving and SwipeState.Swiping states differently to exit to default state properly. if (swipeState == SwipeState.Moving || swipeState == SwipeState.Activated) { // Track if the current active object is in reach if (activeObject != null && CanStartSwiping(activeObject)) { lastTimeObjectInReach = Time.time; } // Reset when the current active object is out of reach for a short period of time if (lastTimeObjectInReach > 0 && Time.time - lastTimeObjectInReach > timeThreshForReset) { ResetAll(); } } // Note, we handle the case of SwipeState.Swiping in Unity. // When SwipeableObject.OnSwipeAnimStarted event is invoked, ResetAll() will be called. //Debug.Log("swipeState = " + swipeState); }
void Awake() { swipeableObject = this.GetComponent <SwipeableObject>(); defaultPosition = this.transform.position; }