我有一个代码,必须让我能够用手指触摸移动一个2D游戏对象,但它不起作用,我该如何解决它?

问题描述 投票:0回答:1

我想制作一个标记为“桨”的2D游戏对象,只有当我先触摸它时,我的手指绕着屏幕,但没有任何反应它只是静止不动。

void Update()
{
    // detect if there was a touch
    if (Input.touchCount > 0)
    {
        switch (touch.phase)
        {
            //When a touch begins:
            case TouchPhase.Began:
                //get the position of the touch
                touch = Input.GetTouch(0);
                touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                //detect if the touch is on the paddle at the beginning of the touch (if YES then we can move the paddle)
                touchHit = Physics2D.Raycast(touchPosition, Camera.main.transform.forward);
                if (touchHit.collider.tag == "paddle")
                {
                    paddleTouched = true;
                }
                break;
            //When the touch is moving, if it has hit the paddle at the beginning the paddle will move
            case TouchPhase.Moved:
                if (paddleTouched == true)
                {
                    gameObject.transform.position = touchPosition;
                }
                break;
            //When a touch has ended everything ends :3       
            case TouchPhase.Ended:
                paddleTouched = false;
                break;
        }
    }

}
c# android unity3d raycasting
1个回答
0
投票

我应该把这些

touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);

在切换案例陈述之前。

© www.soinside.com 2019 - 2024. All rights reserved.