我想检测鼠标向上滑动和向下滑动,我尝试了以下脚本,但仅能正常工作:
1-如果手指在任意两次滑动之间已松开。 2-如果尚未松开手指,则仅在第二次滑动已超过原始手指位置(firstPressPos)时。
我想要的是:
例如,我将手指放在屏幕上并向下滑动,然后向上滑动(在两次滑动之间没有松开手指)之后,我想实时检测到两次滑动。
我该怎么做?
脚本:
if (Input.GetMouseButtonDown(0))
{
firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButton(0))
{
secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
//Swipe Up
}
if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
//Swipe Down
}
}
答案 0 :(得分:0)
您需要添加几个标志,以了解自己处于哪个滑动状态。
private bool _swiping;
private bool _swipingDown;
private Vector3 _previousSwipePosition;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
_previousSwipePosition = Input.mousePosition;
_swiping = true;
Debug.log("Started Swipe");
}
else if (Input.GetMouseButtonUp(0))
{
_swiping = false;
Debug.log("Finished Swipe");
}
if (_swiping)
{
Vector3 newPosition = Input.mousePosition;
if (newPosition.y < _previousSwipePosition.y)
{
if (!_swipingDown)
{
Debug.Log("Started Swipe Down");
_swipingDown = true;
}
}
if (newPosition.y> _previousSwipePosition.y)
{
if (_swipingDown)
{
Debug.Log("Started Swipe Up");
_swipingDown = false;
}
}
_previousSwipePosition = newPosition;
}
}
这将让您知道何时使用标志开始,完成和更改方向。
标志-用于表示某些内容的布尔值