Repository navigation
Use the 2D collision matrix for NetworkCollider2D when Layers is nothing - #1100
Open
jimdroberts wants to merge 1 commit into
Open
jimdroberts wants to merge 1 commit into
jimdroberts wants to merge 1 commit into
Conversation
With Layers left at nothing, NetworkColliderBase derived the query mask from Layers.GetInteractableLayersValue, which reads Physics.GetIgnoreLayerCollision, the 3D matrix. NetworkTrigger2D and NetworkCollision2D query Physics2D, so they used the wrong matrix and missed or added layers whenever the two matrices differ. Add Layers.GetInteractableLayersValue2D, built from Physics2D.GetIgnoreLayerCollision, and a protected virtual GetInteractableLayersValue on NetworkColliderBase that NetworkCollider2D overrides to use it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
Layersis nothing,NetworkTrigger2D(andNetworkCollision2D, see #1074) derive their query mask from the 3D collision matrix (Project Settings > Physics). They should use the 2D one (Physics 2D). Their queries go throughPhysicsScene2D, so the 2D matrix is the one the user configured for these colliders. In a typical 2D project the 3D matrix is left at its default of every layer colliding with every other layer. A 2D trigger left at nothing then reports objects on every layer and ignores the 2D matrix completely. When both matrices are edited and differ, the trigger also misses layers that the 2D matrix enables.Cause
Runtime/Generated/Component/Prediction/NetworkColliderBase.csL138-152 on main: whenLayersis nothing,TryPrepareColliderCheckusesGameKit.Dependencies.Utilities.Layers.GetInteractableLayersValue(gameObject.layer). The base class has no notion of 2D or 3D. The only split is the subclass,NetworkColliderorNetworkCollider2D, so the same lookup serves both.Runtime/Plugins/GameKit/Dependencies/Utilities/Layers.csL19-38 builds that lookup fromPhysics.GetIgnoreLayerCollision, which reads the 3D matrix only.NetworkCollider2D.csL152-157, L270 and L282 pass the resulting mask toPhysicsScene2D.OverlapCollider/OverlapCircle/OverlapBox.Fix
Layers.GetInteractableLayersValue2D(int layer): the same table and the same lazy initialization as the 3D method, built fromPhysics2D.GetIgnoreLayerCollision.NetworkColliderBasegetsprotected virtual int GetInteractableLayersValue(int layer). It returns the existing 3D lookup and is called where the lookup was called before.NetworkCollider2Doverrides it to return the 2D lookup.NetworkColliderand anything else that derives fromNetworkColliderBasekeep the 3D lookup. A user-defined 2D subclass ofNetworkColliderBasecan now override the method as well.Not changed: both lookups are built once per domain and cached in a static. A later
Physics.IgnoreLayerCollision/Physics2D.IgnoreLayerCollisioncall at runtime is therefore never seen by a collider that relies on the fallback. With domain reload disabled, the same is true of matrix edits made between play sessions. That behavior predates this PR and also applies to the 3D path, so I left it out. Fixing it means either rebuilding per query (32×32 native calls) or adding an invalidation API. Callers that change the matrix at runtime can setLayersexplicitly.Merges. This branch merges cleanly (
git merge-tree --write-tree) with #1074 (restoresNetworkCollision2D.cs), #1085 (NetworkCollider/2D per-tick bookkeeping, which rewrites theCheckCollidersbody) and #1093 (GetLayers/SetLayersonNetworkColliderBase), each on its own and with all three together. The combined tree compiles with no new diagnostics. The edits sit outside the hunks those PRs change: the override goes afterOnDestroyinNetworkCollider2D, and the virtual goes afterTryPrepareColliderCheckin the base. The PRs do not depend on each other. With #1093,SetLayers(0)resets the layer cache, and the next check goes through the new virtual and picks the right matrix.Evidence
Compiled against Unity 6000.4 with no new errors or warnings, both on its own and merged with #1074, #1085 and #1093. Not run in play mode.
Behavior change
NetworkCollider2Dcomponents whoseLayersis nothing now use the 2D collision matrix. Projects whose two matrices agree see no difference.Layersvalues and all 3D components are unchanged.Layers.GetInteractableLayersValue2Dand the protected virtualNetworkColliderBase.GetInteractableLayersValue.