.net c#检测滑动方向的逻辑

Logic for detecting swipe direction
2020-11-24
  •  译文(汉语)
  •  原文(英语)

基本上,我想做的是创建一些代码,这些代码将在触摸屏幕上滑动时进行注册,因此我可以通过触摸控件移动角色.

我写的东西有时似乎只起作用,当它起作用时,会使角色多次移动.

这是我的代码:

if (Input.touches [0].phase == TouchPhase.Ended) {

        Debug.Log (Input.touches [0].deltaPosition);
        if (Input.touches[0].deltaPosition.y > Input.touches[0].deltaPosition.x || Input.touches[0].deltaPosition.y > (-Input.touches[0].deltaPosition.x))
            movePlayer ("Up");

        if ((-Input.touches[0].deltaPosition.y) > Input.touches[0].deltaPosition.x || (-Input.touches[0].deltaPosition.y) > (-Input.touches[0].deltaPosition.x))
            movePlayer ("Down");

        if ((-Input.touches[0].deltaPosition.x) > Input.touches[0].deltaPosition.y || (-Input.touches[0].deltaPosition.x) > (-Input.touches[0].deltaPosition.y))
            movePlayer ("Left");

        if (Input.touches[0].deltaPosition.x > Input.touches[0].deltaPosition.y || Input.touches[0].deltaPosition.x > (-Input.touches[0].deltaPosition.y))
            movePlayer ("Right");

    }

任何建议都会非常有帮助

速聊1:
向上的情况下... y>x && y>-x?换句话说,您在这里测试什么?
速聊2:
是的,这是我的困扰,因为我很烦,但是将其更改为||.而不是&&似乎什么也没做
解决过程1

做出决定后,您无需再运行测试,因此这是使用else ifelse

if()
{
}
else if()
{
}
else
{
}

以确保只有一个案件可以执行.

但是...我认为您的测试存在缺陷.以下内容是否更合适?

var dp = Input.touches[0].deltaPosition;
if(Math.Abs(dp.x) > Math.Abs(dp.y))
{
    //gesture had more horizonal movement
    if(dp.x < 0)
    {
        //left
    }
    else
    {
        //right
    }
}
else
{
    //gesture had more vertical movement
    if(dp.y < 0)
    {
        //up
    }
    else
    {
        //down
    }
}
速聊1:
好的,谢谢,这看起来比我的要好得多.这听起来可能很愚蠢,但是Math.Abs​​会做什么?它只是使它变得积极吗?
速聊2:
是的,它消除了负数的负号,因此我们正在测试哪个维度具有更大的"大小",而不管它是正数还是负数.
速聊3:
这将很容易知道,因为它使这样的事情变得容易得多.无论如何,谢谢您的帮助
解决过程2

我写了这个简单的教程,基本上是7行代码,并且很吸引人.简单容易.您只需要在Unity事件系统中使用te build来编写冗长且无用的代码. 无需使用更新或固定更新.

Basically what I am trying to do is create some code that will register when a touch has swiped across the screen, so I can move my character by touch controls.

What I have written only seems to work sometimes, and when it does it makes the character move multiple times.

Here is my code :

if (Input.touches [0].phase == TouchPhase.Ended) {

        Debug.Log (Input.touches [0].deltaPosition);
        if (Input.touches[0].deltaPosition.y > Input.touches[0].deltaPosition.x || Input.touches[0].deltaPosition.y > (-Input.touches[0].deltaPosition.x))
            movePlayer ("Up");

        if ((-Input.touches[0].deltaPosition.y) > Input.touches[0].deltaPosition.x || (-Input.touches[0].deltaPosition.y) > (-Input.touches[0].deltaPosition.x))
            movePlayer ("Down");

        if ((-Input.touches[0].deltaPosition.x) > Input.touches[0].deltaPosition.y || (-Input.touches[0].deltaPosition.x) > (-Input.touches[0].deltaPosition.y))
            movePlayer ("Left");

        if (Input.touches[0].deltaPosition.x > Input.touches[0].deltaPosition.y || Input.touches[0].deltaPosition.x > (-Input.touches[0].deltaPosition.y))
            movePlayer ("Right");

    }

Any advice would be really helpful

Talk1:
The up case... y>x && y>-x? In words, what are you testing here?
Talk2:
OK that was from me messing about because I was getting annoyed, but changing it to || instead of && doesn't seem to do anything
Solutions1

Once you've made a decision, you don't need to run anymore tests, so this would be an ideal time for using else if and else

if()
{
}
else if()
{
}
else
{
}

to ensure that only a single case could execute.

However... I think your testing is flawed. Wouldn't the following be more appropriate?

var dp = Input.touches[0].deltaPosition;
if(Math.Abs(dp.x) > Math.Abs(dp.y))
{
    //gesture had more horizonal movement
    if(dp.x < 0)
    {
        //left
    }
    else
    {
        //right
    }
}
else
{
    //gesture had more vertical movement
    if(dp.y < 0)
    {
        //up
    }
    else
    {
        //down
    }
}
Talk1:
Ok thanks that looks a lot better than mine. This might sound stupid but what does Math.Abs do? Does it just make it positive?
Talk2:
Yes, it removes the minus of a negative number, so we're testing which dimension has more "size" irrespective of whether it's positive or negative.
Talk3:
That'll be handy to know as it makes something like this a lot easier. Anyway thanks for your help
Solutions2

I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code. No need to use an update or fixed update.

转载于:https://stackoverflow.com/questions/28157845/logic-for-detecting-swipe-direction

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入