Индекс выходит за границы, когда я продолжаю нажимать пальцем - PullRequest
0 голосов
/ 18 мая 2019

Метка "mShape" - это желтые фигуры.

Game Work Когда: * Мяч не будет создан, если вы нажмете на желтую фигуру. * Нажмите на красное пространство, чтобы создать шар (PointA). Когда я двигаю пальцем, я вижу путь точек как направление (PointB). Когда я отпускаю палец, Ball будет двигаться.

Игра не работает, когда: когда я продолжаю нажимать пальцем на желтую фигуру и тащить пальцем касание. Я получил ошибку.

* Я использовал ресурсы "Просмотр журнала", чтобы обнаружить ошибку, когда играю на моем устройстве.

CHECK BUG IMAGE

CHECK BUG2 IMAGE

if (ShowDir)
{
    pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    pointB.z = Ball.transform.position.z;

    if (DirDectect)
    {

        if (DottedLine.DottedLine.angle < 92 && DottedLine.DottedLine.angle > 88)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
            pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 272 && DottedLine.DottedLine.angle > 268)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
            pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 2 || DottedLine.DottedLine.angle > 358)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
            pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);
            //DirDectect = false; 
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 182 && DottedLine.DottedLine.angle > 178)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
            pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }
    }

    if (DottedLine.DottedLine.angle > 2 && DottedLine.DottedLine.angle < 88 ||
        DottedLine.DottedLine.angle > 92 && DottedLine.DottedLine.angle < 178 ||
        DottedLine.DottedLine.angle > 182 && DottedLine.DottedLine.angle < 268 ||
        DottedLine.DottedLine.angle > 272 && DottedLine.DottedLine.angle < 358)
    {

        DirDectect = true;
    }

    DottedLine.DottedLine.Instance.DrawDottedLine(pointA, pointB);
}

if (Input.touchCount > 0)
{
    Touch touch = Input.GetTouch(0);

    if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
         Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         RaycastHit2D hit = Physics2D.Raycast(worldPoint, Camera.main.transform.forward);

         if (hit.collider.tag != null)
         {
             if (touch.phase == TouchPhase.Began && hit.collider.tag != "mShape")
             {
                 pointA = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                 MyBall = Instantiate(Ball, new Vector2(pointA.x, pointA.y), Quaternion.identity);
             }

             if (touch.phase == TouchPhase.Moved && hit.collider.tag != "mShape")
             {
                 ShowDir = true;
             }

             if (touch.phase == TouchPhase.Ended && hit.collider.tag != "mShape")
             {

                 if (wrapActive)
                 {
                     MyBall.AddComponent<wrap>();
                 }

                 MyBall.GetComponent<Rigidbody2D>().velocity = (pointB - MyBall.transform.position).normalized * speed;

                 HintShow.SetActive(false);
                 HintBtn.interactable = false;
                 SkipBtn.interactable = false;

                 ShowDir = false;
                 DesTime = true;
             }
         }  
    }
}
else
{
    Debug.Log("NULL");
}

РЕШЕНИЕ

           if(Input.touchCount > 0 )
        {
            Touch touch = Input.GetTouch(0);

            if(ShowDir)
        {
            pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            pointB.z = Ball.transform.position.z;

            if (DirDectect)
            {

                if (DottedLine.DottedLine.angle < 92 && DottedLine.DottedLine.angle > 88)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
                    pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 272 && DottedLine.DottedLine.angle > 268)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
                    pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 2 || DottedLine.DottedLine.angle > 358)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
                    pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 182 && DottedLine.DottedLine.angle > 178)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
                    pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }
            }

            if (DottedLine.DottedLine.angle > 2 && DottedLine.DottedLine.angle < 88 ||
                DottedLine.DottedLine.angle > 92 && DottedLine.DottedLine.angle < 178 ||
                DottedLine.DottedLine.angle > 182 && DottedLine.DottedLine.angle < 268 ||
                DottedLine.DottedLine.angle > 272 && DottedLine.DottedLine.angle < 358)
            {

                DirDectect = true;
            }

            DottedLine.DottedLine.Instance.DrawDottedLine(pointA, pointB);

        }

            if(!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)){

                Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                RaycastHit2D hit = Physics2D.Raycast(worldPoint, Camera.main.transform.forward);

                 if (hit.collider.tag != null ){

                        //BEGIN
                        if (touch.phase == TouchPhase.Began && hit.collider.tag != "mShape" && MoveFalse )
                        {
                            BlockCode = false;
                            pointA = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                            MyBall = Instantiate(Ball, new Vector2(pointA.x, pointA.y), Quaternion.identity);
                        }

                        if (touch.phase == TouchPhase.Began && hit.collider.tag == "mShape" && MoveFalse)
                        {
                           BlockCode = true;
                        }

                        if (!BlockCode)
                    {
                    //MOVE
                    if (touch.phase == TouchPhase.Moved && hit.collider.tag != "mShape")
                    {

                        ShowDir = true;
                        mShapeClicked = false;
                        MoveFalse = false;
                        Debug.Log("MOVE");
                    }

                    if (touch.phase == TouchPhase.Moved && hit.collider.tag == "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = true;
                        MoveFalse = false;
                        Debug.Log("OverShape");
                    }

                    //NO-MOVE
                    if (touch.phase == TouchPhase.Stationary && hit.collider.tag != "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = false;
                        MoveFalse = false;
                        Debug.Log("NoMove");
                    }

                    if (touch.phase == TouchPhase.Stationary && hit.collider.tag == "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = true;
                        MoveFalse = false;
                        Debug.Log("NoMove");
                    }

                    //END 
                    if (touch.phase == TouchPhase.Ended)
                    {
                        MoveFalse = false;
                        mShapeClicked = false;
                        ShowDir = false;
                        DesTime = true;

                        Debug.Log("END");

                        if (wrapActive)
                        {
                            MyBall.AddComponent<wrap>();
                        }

                        if (!mShapeClicked)
                        {
                            MyBall.GetComponent<Rigidbody2D>().velocity = (pointB - MyBall.transform.position).normalized * speed;
                        }

                        HintShow.SetActive(false);
                        HintBtn.interactable = false;
                        SkipBtn.interactable = false;



                    }

                   }
                 }

            }
        }

1 Ответ

1 голос
/ 18 мая 2019

Вы получаете ошибку «вне границ» при вызове функции Input.GetTouch(0). Это означает, что вы пытаетесь получить информацию о первом касании, которое происходит, когда никаких касаний не происходит. Вам нужно будет изменить свой код, чтобы сначала убедиться, что есть хотя бы одно активное касание, используя Input.touchCount

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...