У меня есть код, который позволяет мне перемещать 2d игровой объект одним касанием пальца, но он не работает, как я могу это исправить? - PullRequest
0 голосов
/ 24 марта 2019

Я хочу, чтобы 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;
        }
    }

}

1 Ответ

0 голосов
/ 24 марта 2019

Я должен был поставить эти

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

до операторов регистра переключателя.

...