diff --git a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/RaycastSystem/ECSRaycastSystem.cs b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/RaycastSystem/ECSRaycastSystem.cs index 029ccb4618..a22444b6da 100644 --- a/unity-renderer/Assets/DCLPlugins/ECS7/Systems/RaycastSystem/ECSRaycastSystem.cs +++ b/unity-renderer/Assets/DCLPlugins/ECS7/Systems/RaycastSystem/ECSRaycastSystem.cs @@ -210,21 +210,32 @@ private Ray CreateRay(IParcelScene scene, IDCLEntity entity, PBRaycast model) private RaycastHit CreateSDKRaycastHit(IParcelScene scene, PBRaycast model, UnityEngine.RaycastHit unityRaycastHit, KeyValuePair? hitEntity, Vector3 globalOrigin) { - if (hitEntity == null) return null; - - // TODO: figure out how we can cache or pool this hit instance to reduce allocations. - // since is part of a protobuf message it life span is uncertain, it could be disposed - // after message is sent to the scene or dropped for a new message - RaycastHit hit = new RaycastHit(); - IDCLEntity entity = hitEntity.Value.Key; - uint collisionMask = hitEntity.Value.Value; - - // hitEntity has to be evaluated since 'Default' layer represents a combination of ClPointer - // and ClPhysics, and 'SDKCustomLayer' layer represents 8 different SDK layers: ClCustom1~8 - if ((model.GetCollisionMask() & collisionMask) == 0) + RaycastHit hit = null; + uint modelCollisionLayerMask = model.GetCollisionMask(); + + if (hitEntity != null) // SDK7 entity, otherwise the ray hit an SDK6 entity + { + // TODO: figure out how we can cache or pool this hit instance to reduce allocations. + // since is part of a protobuf message it life span is uncertain, it could be disposed + // after message is sent to the scene or dropped for a new message + IDCLEntity entity = hitEntity.Value.Key; + uint collisionMask = hitEntity.Value.Value; + + // hitEntity has to be evaluated since 'Default' layer represents a combination of ClPointer + // and ClPhysics, and 'SDKCustomLayer' layer represents 8 different SDK layers: ClCustom1~8 + if ((modelCollisionLayerMask & collisionMask) == 0) + return null; + + hit = new RaycastHit(); + hit.EntityId = (uint)entity.entityId; + } + else if ((!scene.isPersistent && !scene.isPortableExperience) + || (modelCollisionLayerMask & (int)ColliderLayer.ClPhysics) == 0) // 'Physics' layer for non-sdk7 colliders + { return null; + } - hit.EntityId = (uint)entity.entityId; + hit ??= new RaycastHit(); hit.MeshName = unityRaycastHit.collider.name; hit.Length = unityRaycastHit.distance; hit.GlobalOrigin = globalOrigin;