
[ad_1]
I’m making an attempt to permit the person to pull and swipe throughout a parallax scene in a seamless vogue. Customers ought to have the ability to:
- Drag the scene from left to proper
- On releasing the drag, the digicam ought to preserve transferring if the final motion on the display screen had enough velocity
- In the event that they swipe as a substitute of dragging, the digicam ought to transfer within the path of the swipe at a pace that displays the speed of the swipe
Reference: I would like 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 preserve transferring 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:
personal void Replace()
{
if (Contact.activeFingers.Depend == 1)
{
swipeAllowed = true;
var contact = Contact.activeTouches[0];
if (contact.section == TouchPhase.Started)
{
startPos = Digicam.primary.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 = Digicam.primary.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 (rework.place != currentPos && swipeAllowed) // has it been interrupted by a drag?
{
rework.place = Vector3.MoveTowards(rework.place, currentPos, pace * Time.deltaTime);
rework.place = new Vector3(Mathf.Clamp(rework.place.x, leftLimit, rightLimit), rework.place.y, rework.place.z);
yield return null;
}
yield return null;
panAllowed = true;
}
IEnumerator DragCamera(Vector3 startPos, Vector3 currentPos)
{
swipeAllowed = false;
distance = currentPos - startPos;
rework.place += new Vector3(-distance.x, 0, 0);
rework.place = new Vector3(Mathf.Clamp(rework.place.x, leftLimit, rightLimit), rework.place.y, rework.place.z);
yield return null;
}
personal 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 person decides mid-drag that they need to launch the drag at a excessive velocity (basically a swipe with their finger nonetheless on the display screen), the digicam will account for the speed of that final motion by transferring at larger pace (proper now I am utilizing rework.place, perhaps I ought to do one thing else)? I additionally need to have the ability to do all the issues I listed above.
Any thought of the place I am going improper with this? How can I do that simply?
[ad_2]