Tuesday, August 9, 2022
HomeGame DevelopmentHow do I add pressure to the digicam when the drag/swipe is...

How do I add pressure to the digicam when the drag/swipe is finished in Unity?


I’m attempting to permit the consumer to tug and swipe throughout a parallax scene in a seamless style. Customers ought to have the ability to:

  • Drag the scene from left to proper
  • On releasing the drag, the digicam ought to maintain shifting if the final motion on the display had enough velocity
  • In the event that they swipe as an alternative of dragging, the digicam ought to transfer within the route of the swipe at a velocity that displays the rate of the swipe

Reference: I need it to seem like this: https://imgur.com/a/H0X3VJm

Discover that I can drag it, after which if I drag and launch at a sure velocity, the digicam will maintain shifting for a little bit of time earlier than stopping. If I merely drag and cease at low velocity, then it will not transfer additional.

Here is what I’ve thus far:

non-public void Replace()
    {
        if (Contact.activeFingers.Rely == 1)
        {
            swipeAllowed = true;
            var contact = Contact.activeTouches[0];

            if (contact.section == TouchPhase.Started)
            {
                startPos = Digital camera.fundamental.ScreenToWorldPoint(contact.screenPosition);
                StartCoroutine(CheckForAnimation(contact));
            }

            else if (contact.section == TouchPhase.Stationary)
            {
                    DragAndDrop(contact);
            }

            else if (contact.section == TouchPhase.Moved && panAllowed)
            {
                if (!CheckForSwipe(contact.startTime))
                {
                    //Debug.Log("drag");
                    currentPos = Digital camera.fundamental.ScreenToWorldPoint(contact.screenPosition);
                    StartCoroutine(DragCamera(startPos, currentPos));
                }
            }

            else if (contact.section == TouchPhase.Ended && swipeAllowed)
            {
                if (CheckForSwipe(contact.startTime))
                {
                    //Debug.Log("swipe");
                    currentPos = startPos;
                    currentPos.y = cameraFixedPosition.y;
                    currentPos.z = cameraFixedPosition.z;
                    StartCoroutine(SwipeCamera(currentPos));
                }
            }
        }
    }

    IEnumerator SwipeCamera(Vector3 currentPos)
    {
        currentPos.x = currentPos.x * 1.5f;
        whereas (remodel.place != currentPos && swipeAllowed) // has it been interrupted by a drag?
        {
            remodel.place = Vector3.MoveTowards(remodel.place, currentPos, velocity * Time.deltaTime);
            remodel.place = new Vector3(Mathf.Clamp(remodel.place.x, leftLimit, rightLimit), remodel.place.y, remodel.place.z);
            yield return null;
        }
        yield return null;
        panAllowed = true;
    }

    IEnumerator DragCamera(Vector3 startPos, Vector3 currentPos)
    {
        swipeAllowed = false;
        distance = currentPos - startPos;
        remodel.place += new Vector3(-distance.x, 0, 0);
        remodel.place = new Vector3(Mathf.Clamp(remodel.place.x, leftLimit, rightLimit), remodel.place.y, remodel.place.z);
        yield return null;
    }

non-public bool CheckForSwipe(double startTime)
    {
        return ((Time.realtimeSinceStartup - startTime) < minTimeToDrag && (startPos != currentPos));
    }

The issue is that this code EITHER drags OR swipes. I do not know to make it in order that if the consumer decides mid-drag that they need to launch the drag at a excessive velocity (basically a swipe with their finger nonetheless on the display), the digicam will account for the rate of that final motion by shifting at greater velocity (proper now I am utilizing remodel.place, possibly I ought to do one thing else)? I additionally need to have the ability to do the entire issues I listed above.

Any concept of the place I am going incorrect with this? How can I do that simply?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments