0
Fixed

Action Store Mouse World Position sorting glitch

Einar 5 months ago in Game Creator updated by Marti (Lead Developer) 5 months ago 7

When using the Store Mouse World Position-action, it is not guaranteed to return the closest gameobject with a collider.  Changing the asset code to use Physics.RayCast instead of Physics.RayCastNonAlloc fixed it in my case. Reference used: https://forum.unity.com/threads/raycastnonalloc-sort-collision-from-furthest-to-closest.588013/ 

Unity version:
2019.3.9f1
Game Creator version:
1.1.6
GOOD, I'M SATISFIED
Satisfaction mark by Einar 5 months ago
Under review

Hmm not sure if that's true. Using Physics.Raycast (not Raycast All) returns the first collided object from the source position. The first one will always be the closest hit.

RaycastNonAlloc avoids generating new heap allocations that will later be collected by the garbage collector. It's a good idea to use that if you plan on using that method very often (once per frame, for example).

+1

From the forum post I linked it seems that RaycastNonAlloc, despite the naming, is in fact equivalent to RaycastAll, not Raycast. And RaycastAll does not guarantee any sorted order. In my case sometimes the second object in the ray's path got hit before the first with RaycastNonAlloc, but with Raycast the first one got hit.

It was easy enough to change for my specific needs, and I don't want to force additional garbage collection on anyone else using the asset. After all, most games don't intentionally throw a lot of clickable colliders close together like a puzzle box game does.

By Design

No worries :-) We use the RaycastNonAlloc in some places where that could become a bottleneck. Feel free to create a custom Action, so that your modified Action version doesn't get overwritten the next time you update Game Creator.

+1

Turns out RaycastNonAlloc is used in other actions/triggers as well, and when used with a RaycastHit array of size 1, it is plain broken, since the result is NOT guaranteed to be sorted. I can still write my own actions and triggers to fix it, but the fact remains that the core functionality that is By Design is actually buggy and unreliable. It's also tiresome to have to contend with something so simple. Mouse raycasts absolutely demands a solution that returns the first sorted hit, and RaycastNonAlloc is the wrong answer.

+1
Planned

Agree, I experimented this first hand in a bug on the Behavior module. I'll open a ticket and address this. Despite this, I'm facing the dilema on choosing the lesser of two evils: Either use Raycast, which is more performant but generates garbage that later needs to be collected, or use RaycastNonAlloc, which gets a bunch of objects (hence, it's less performant) but generates no garbage.

I'll most likely change to Raycast for simplicity in those cases where it's not performed every frame.

+1
Fixed

I've changed all NonAlloc versions of raycasts that just check for one collision to use the default one. It generates a bit of garbage, but since these are sparingly used, shouldn't be a problem unless you run your game on a potato. This will be available in the next update. If there are any further issues, feel free to reopen this thread.