public void OnWorldPosChanged(ref MatrixD newTransform) { MatrixD worldPos = newTransform; m_caster.OnWorldPositionChanged(ref worldPos); var entitiesInRange = this.m_caster.EntitiesInRange; float closestDistance = float.MaxValue; MyEntity closestEntity = null; if (!m_isPointOfRefSet) m_pointOfReference = worldPos.Translation; if (entitiesInRange != null && entitiesInRange.Count > 0) { // int i = 0; foreach (var entity in entitiesInRange.Values) { float distanceSq = (float)Vector3D.DistanceSquared(entity.DetectionPoint, m_pointOfReference); if (entity.Entity.Physics != null && entity.Entity.Physics.Enabled) { if (distanceSq < closestDistance) { closestEntity = entity.Entity; this.m_distanceToHitSq = distanceSq; this.m_hitPosition = entity.DetectionPoint; closestDistance = distanceSq; } } // ++i; } } this.m_hitCubeGrid = closestEntity as MyCubeGrid; this.m_hitBlock = null; this.m_hitDestroaybleObj = closestEntity as IMyDestroyableObject; this.m_hitFloatingObject = closestEntity as MyFloatingObject; this.m_hitCharacter = closestEntity as MyCharacter; if (m_hitCubeGrid != null) { var invWorld = m_hitCubeGrid.PositionComp.WorldMatrixNormalizedInv; var gridLocalPos = Vector3D.Transform(this.m_hitPosition, invWorld); Vector3I blockPos; m_hitCubeGrid.FixTargetCube(out blockPos, gridLocalPos / m_hitCubeGrid.GridSize); m_hitBlock = m_hitCubeGrid.GetCubeBlock(blockPos); } }