Touch coordinates for UNI.T. Build? Collider problem.

Asked 2 years ago, Updated 2 years ago, 112 views

    Ray ray;
    RaycastHit2D hit;

      void Update()
    {
        if (latestPanel != null)
        {
            if (Input.GetMouseButtonDown(0))
            {
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                hit = Physics2D.Raycast(ray.origin, ray.direction);

                if (hit.collider == null || hit.collider.name != latestPanel.name)
                {
                    i++;
                    latestPanel.GetComponent<panelScript>().Close(i);
                    if (i == 2)
                    {
                        i = 1;
                    }
                }
                else
                {
                }
            }
        }

You want to turn off panels such as Options Pane when you open them and click any part other than the window.

Unity does it without any problems, but on mobile after build, there are cases where it does not turn off even if you press the window, as if the color slider is in another part, or it does not turn off even if you press the non-window.

In the lower part, the penguin shape and its window are optional windows, and the size of the window is applied with a collider.

So, if there is a collider at the time of touch, the window will not turn off, and if there is no collider (if it is the background), the window will not turn off.

However, after building on mobile, there is a situation where it turns off unconditionally when you click on the red part below.

It's as if the object is still there, but the collider offset is pushed northeast.

The same problem occurs in all scenes, but it works fine in the editor.

What's the problem?

unity

2022-09-22 08:15

1 Answers

Why don't you print out Debug.Log(hit.collider.name); to see if the collider really moved sideways?

By the way, I think it is too inefficient to use Raycast to process through Update to implement UI. The list of lectures on the official site is in English, but I recommend the videos because they have Korean subtitles. https://unity3d.com/kr/learn/tutorials/topics/user-interface-ui

If you have already drawn the screen with UI Components such as Canvas, it would be better to use EventTrigger on the image that covers the background rather than using raycast to check that you touch off the screen.


2022-09-22 08:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.