- Home /
Page swiping goes to blank space too
So my problem is that when I'm swiping, it goes to blank pages too (the blue basic background). I tried to fixed it for myself but I can only manage to do that if you go to blank space it switches you to the nearest panel automatically.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SwipPages : MonoBehaviour, IDragHandler, IEndDragHandler
{
private Vector3 panelLocation;
public float percentThreshold = 0.2f;
public float easing = 0.5f;
public int totalPages = 1;
private int currentPage = 1;
void Start()
{
panelLocation = transform.position;
}
public void OnDrag(PointerEventData data)
{
float difference = data.pressPosition.x - data.position.x;
transform.position = panelLocation - new Vector3(difference, 0, 0);
}
public void OnEndDrag(PointerEventData data)
{
float percentage = (data.pressPosition.x - data.position.x) / Screen.width;
if (Mathf.Abs(percentage) >= percentThreshold)
{
Vector3 newLocation = panelLocation;
if (percentage > 0 && currentPage <totalPages)
{
currentPage++;
newLocation += new Vector3(-Screen.width, 0, 0);
}
else if (percentage < 0 && currentPage > 1)
{
currentPage--;
newLocation += new Vector3(Screen.width, 0, 0);
}
StartCoroutine(SmoothMove(transform.position, newLocation, easing));
panelLocation = newLocation;
}
else
{
StartCoroutine(SmoothMove(transform.position, panelLocation, easing));
}
}
IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds)
{
float t = 0f;
while (t <= 1.0)
{
t += Time.deltaTime / seconds;
transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t));
yield return null;
}
}
}
thing.png
(15.6 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613002410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
U-shaped swipe 0 Answers
getting udp package info inside unity (GlovePIE) 0 Answers
Mouse Swipe and Code Efficiency 0 Answers
Objective Pointer 0 Answers
Missing parts when I build my game.... 2 Answers