コード例 #1
0
        private void VerticalCollisionDetect()
        {
            int detectionCount = 1;

            if (_movement.y == 0)
            {
                if (this.collisionDetectionMode != CollisionDetectionMode.WhenMoving)
                {
                    detectionCount = 2;
                }
            }

            var directionY = _movement.y > 0 ? 1 : -1;

            for (int d = 0; d < detectionCount; d++)
            {
                directionY = d == 0 ? directionY : -directionY;
                var rayOrigin = (directionY == 1) ? _raycastOrigins.topLeft : _raycastOrigins.bottomLeft;
                rayOrigin.x += _movement.x;

                var rayLength = Mathf.Abs(_movement.y) + _shrinkWidth;
                if (_movement.y == 0f)
                {
                    rayLength += _expandWidth;
                }
                for (int i = 0; i < this.verticalRayCount; i++)
                {
                    _raycastDirection.x  = 0.0f;
                    _raycastDirection.y  = 1.0f;
                    _raycastDirection.x *= directionY;
                    _raycastDirection.y *= directionY;
                    if (JPhysicsManager.useUnityRayCast)
                    {
                        var hitCount = Physics2D.RaycastNonAlloc(rayOrigin, _raycastDirection, _raycastHit2D, rayLength, this.collisionMask);
                        for (int j = 0; j < hitCount; j++)
                        {
                            var hit = _raycastHit2D[j];

                            var hitCollider = hit.collider;
                            // Ignored Collider?
                            if (_ignoredColliders.Contains(hitCollider))
                            {
                                continue;
                            }
                            HandleVerticalHitResult(hit.collider, hit.point, hit.distance, directionY);
                        }
                    }
                    else
                    {
                        _jraycastHitList.Clear();
                        JPhysics.Raycast(JPhysicsManager.instance.quadTree, rayOrigin, _raycastDirection, ref _jraycastHitList, rayLength, this.collisionMask);
                        for (int j = 0; j < _jraycastHitList.count; j++)
                        {
                            var hit = _jraycastHitList[j];
                            if (_ignoredColliders.Contains(hit.collider))
                            {
                                continue;
                            }
                            HandleVerticalHitResult(hit.collider, hit.point, hit.distance, directionY);
                        }
                    }

                    rayOrigin.x += _verticalRaySpace;
                }
            }
        }
コード例 #2
0
        private void HorizontalCollisionDetect()
        {
            int detectionCount = 1;

            if (_movement.x == 0)
            {
                if (this.collisionDetectionMode == CollisionDetectionMode.WhenMoving)
                {
                    return;
                }
                detectionCount = 2;
            }

            var directionX = _movement.x >= 0 ? 1 : -1;

            for (int d = 0; d < detectionCount; d++)
            {
                directionX = d == 0 ? directionX : -directionX;
                var rayOrigin = (directionX == 1) ? _raycastOrigins.bottomRight : _raycastOrigins.bottomLeft;
                var rayLength = Mathf.Abs(_movement.x) + _shrinkWidth;
                if (_movement.x == 0f)
                {
                    rayLength += _expandWidth;
                }

                for (int i = 0; i < this.horizontalRayCount; i++)
                {
                    _raycastDirection.x  = 1.0f;
                    _raycastDirection.y  = 0.0f;
                    _raycastDirection.x *= directionX;
                    _raycastDirection.y *= directionX;

                    if (JPhysicsManager.useUnityRayCast)
                    {
                        var hitCount = Physics2D.RaycastNonAlloc(rayOrigin, _raycastDirection, _raycastHit2D, rayLength, this.collisionMask);
                        for (int j = 0; j < hitCount; j++)
                        {
                            var hit = _raycastHit2D[j];
                            if (_ignoredColliders.Contains(hit.collider))
                            {
                                continue;
                            }
                            HandleHorizontalHitResult(hit.collider, hit.point, hit.distance, directionX);
                        }
                    }
                    else
                    {
                        _jraycastHitList.Clear();
                        JPhysics.Raycast(JPhysicsManager.instance.quadTree, rayOrigin, _raycastDirection, ref _jraycastHitList, rayLength, this.collisionMask);
                        for (int j = 0; j < _jraycastHitList.count; j++)
                        {
                            var hit = _jraycastHitList[j];
                            if (_ignoredColliders.Contains(hit.collider))
                            {
                                continue;
                            }
                            HandleHorizontalHitResult(hit.collider, hit.point, hit.distance, directionX);
                        }
                    }
                    rayOrigin.y += _horizontalRaySpace;
                }
            }
        }