0
\$\begingroup\$

Update 2 (see below)

I'm writing a turn-based game. During setup phase time scale modifier equals to 0, what prevents running of any physics-based code and FixedUpdates. User have freezed action and may make decisions.

The problem is that sometimes I cannot select objects during the setup mode. I'm doing selection in the following way:

  • I obtain mouse position ray
  • I run RaycastAll against this ray without limitations
  • I check for hit objects, which has Selectable script on them. Then rest of the logic happens.

Mouse handling looks like following:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

(...)

RaycastHit[] hits = Physics.RaycastAll(ray);

// DEBUG
string debug = "Raycast all gave " + hits.Length + " hits:";
for (int i = 0; i < hits.Length; i++)
    debug += hits[i].collider.gameObject.name + "; ";
Debug.Log(debug);
// END DEBUG

Sometimes though I'm not getting valid results from RaycastAll. Take a look at the following example:

Error

I clicked top-left blue ship. You may see a small sphere on it - I wrote a debug code, which generates a series of spheres along the ray to make sure, that it is valid. Since you see only one sphere here, it seems like everything's OK (note that I added this code to diagnose the problem. Spheres surely aren't causing it)

The scene view looks like following:

Ray pointing to ship

It should be clear on this image, that ray hits the ship's box collider. In this specific case, a couple of units next to ship is an explosion, which has bigger sphere collider.

Explosion's collider

You can also see debug log from the session, which clearly says, that RaycastAll only hit explosion and it missed the ship, which is just next to it.

Now the question is: why RaycastAll doesn't return the ship along with the explosion?

Note, that this problem happens more often and without near objects with colliders, sometimes I just cannot select a ship and all I have to do is to move camera, such that object is closer to center of the screen.


Edit: Now this gets interesting!

I added some debugging. After the RaycastAll, I manually go over all colliders in the scene and verify the raycast. The code now looks like following:

        RaycastHit[] hits = Physics.RaycastAll(ray);

        // DEBUG
        string debug = "Raycast all gave " + hits.Length + " hits:";
        for (int i = 0; i < hits.Length; i++)
            debug += hits[i].collider.gameObject.name + "; ";
        Debug.Log(debug);

        var colliders = GameObject.FindObjectsOfType<Collider>();
        foreach (var collider in colliders)
        {
            RaycastHit temp;
            if (collider.Raycast(ray, out temp, float.PositiveInfinity))
            {
                Debug.Log("Manually verified, that ray hits " + collider.gameObject.name);
            }
        }

        // END DEBUG

And when I try to click the "un-raycastable" object, I get:

Raycast all gave 1 hits:MissileExplosion(Clone);
Manually verified, that ray hits MissileExplosion(Clone)
Manually verified, that ray hits Falcon(Clone)

A bug in Unity engine?


Edit: Yup, a bug in Unity engine.

Hey,

We have been able to reproduce this bug and have sent it for resolution with our developers.

Regards, Unity QA

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This seems to be a bug in Unity engine. It has been sent to Unity devs for resolution and hopefully a fix will be in place in next Unity version.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .