【问题标题】:How to get (0,0) from the center of the game in unity如何统一从游戏中心获得(0,0)
【发布时间】:2019-09-16 14:50:09
【问题描述】:

我是 Unity 新手,我正在尝试使用 Input.touch[0] 为移动设备进行滑动输入。我正在触摸点,并使用它来计算滑动增量,但问题是输入坐标来自屏幕的左角。我需要屏幕中心的 0,0。这是我的脚本:

    public class SwipeController : MonoBehaviour
    {
    private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    private bool isDragging;
    private Vector2 startTouch, swipeDelta;

    private void Update()
        {
        tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

    //stand Alone Inputs

    if (Input.GetMouseButtonDown(0))
    {
        isDragging = true;
        tap = true;
        startTouch = Input.mousePosition;
    } else if (Input.GetMouseButtonUp(0)) {
        isDragging = false;
        Reset();
    }


    //MOBILE INPUTS

    if (Input.touches.Length > 0 )
    {
        if(Input.touches[0].phase == TouchPhase.Began)
        {
            tap = true;
            isDragging = true;
            startTouch = Input.touches[0].position;
        } else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            isDragging = false;
            Reset();
        }
    }

    //Distance calcs

    if (isDragging)
    {
        if(Input.touches.Length > 0)
        {
            swipeDelta = Input.touches[0].position - startTouch;
        } else if (Input.GetMouseButton(0)) {
            swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

        //DEAD ZONE?

    if (swipeDelta.magnitude > 100)
        {
            float x = swipeDelta.x;
            float y = swipeDelta.y;

            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                //left or right
                if (x < 0)
                {
                    swipeLeft = true;
                } else
                {
                    swipeRight = true;
                }
            } else
            {
                // top or bottom
                if (y < 0 )
                {
                    swipeDown = true;
                } else
                {
                    swipeUp = true;
                }
            }

            Reset();
        }
    }
}

    private void Reset()
    {
       startTouch = swipeDelta = Vector2.zero;
    }

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }
    }

我错过了什么?

【问题讨论】:

  • 如果您只关心增量,那么屏幕的哪个部分是原点并不重要。您能否更详细地描述当前行为与预期行为?
  • 偏移屏幕分辨率的一半宽度和一半高度?

标签: c# unity3d


【解决方案1】:

您正在寻找 Camera.ScreenToWorldPoint。

触摸输入以像素而非世界单位记录。如果您将触摸位置输入相机的ScreenToWorldPoint 方法,则返回的 Vector3 是被触摸的世界位置。通常用户只会使用:

Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));

但建议缓存 Camera.main 引用以提高性能。上面链接的官方文档就是一个很好的例子。

【讨论】:

  • 如果相机处于透视模式,这将始终返回相机的位置,与触摸/鼠标的位置无关。即使相机处于正交模式,这也会返回世界空间中触摸/鼠标位置的投影,而不是像提问者想要的屏幕空间。
  • 我假设提问者不熟悉 Unity 作为引擎,也不熟悉向量背后的数学原理。我假设这是因为在屏幕空间中找到以 0,0 为中心的位置没有任何价值(正如您之前提到的,增量不会改变)。有很多语法错误导致我得出这个答案。这些错误似乎已得到纠正,你是对的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多