/// <summary>
        /// Performs a collision test in any of the four cardinal directions.
        /// </summary>
        public CollisionHitInfo CardinalCollision(CardinalCollisionType cardinalCollisionType, float skin, float extraDistance, LayerMask layerMask)
        {
            CollisionHitInfo hitInfo = new CollisionHitInfo();

            hitInfo.Reset();

            Vector3 origin = Vector3.zero;

            float castDistance = skin + extraDistance;

            Vector3 direction = Vector3.zero;

            Vector3 center = characterBody.bodyTransform.Position + characterBody.bodyTransform.Up * characterBody.heightExtents;

            switch (cardinalCollisionType)
            {
            case CardinalCollisionType.Up:
                direction = characterBody.bodyTransform.Up;
                origin    = center + direction * (characterBody.height / 2 - skin);
                break;

            case CardinalCollisionType.Down:
                direction = -characterBody.bodyTransform.Up;
                origin    = center + direction * (characterBody.height / 2 - skin);
                break;

            case CardinalCollisionType.Left:
                direction = -characterBody.bodyTransform.Right;
                origin    = center + direction * (characterBody.width / 2 - skin);
                break;

            case CardinalCollisionType.Right:
                direction = characterBody.bodyTransform.Right;
                origin    = center + direction * (characterBody.width / 2 - skin);
                break;
            }



            hitInfo = PhysicsUtilities.Raycast(
                is3D,
                origin,
                direction,
                castDistance,
                layerMask
                );



            return(hitInfo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check if the wall slide ability conditions are met (a wall in front of the character with the corresponding "Wall Sliding" Tag).
        /// </summary>
        public bool CheckWallSliding()
        {
            if (characterController2D.IsGrounded)
            {
                return(false);
            }

            if (characterBrain.CharacterAction.down)
            {
                return(false);
            }

            if (characterController2D.Velocity.y > 0)
            {
                return(false);
            }

            CollisionHitInfo info = new CollisionHitInfo();

            info.Reset();

            CardinalCollisionType cardinalCollisionType =
                characterController2D.IsFacingRight ?
                CardinalCollisionType.Right :
                CardinalCollisionType.Left;

            float skin = characterBody.SkinWidth;

            info = characterController2D.CharacterCollisions.CardinalCollision(
                cardinalCollisionType,
                skin,
                skin,
                layerMask
                );

            if (!info.collision)
            {
                return(false);
            }

            float wallSignedAngle = Utilities.SignedAngle(characterBody.bodyTransform.Up, info.normal, characterBody.bodyTransform.Forward);
            float wallAngle       = Mathf.Abs(wallSignedAngle);

            if (!info.gameObject.CompareTag(wallSlideTag) || !Utilities.isCloseTo(wallAngle, 90, 0.1f))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 3
0
        public override void Process(float dt)
        {
            if (!movementController.isCurrentlyOnState(MovementState.Normal))
            {
                return;
            }

            if (!characterController2D.IsGrounded)
            {
                return;
            }

            hitInfo.Reset();

            CardinalCollisionType cardinalCollisionType =
                characterController2D.IsFacingRight ?
                CardinalCollisionType.Right :
                CardinalCollisionType.Left;


            float skin = characterBody.SkinWidth;

            hitInfo = characterController2D.CharacterCollisions.CardinalCollision(
                cardinalCollisionType,
                skin,
                skin,
                wallLayerMask
                );

            if (hitInfo.collision)
            {
                float wallSignedAngle = Utilities.SignedAngle(characterController2D.transform.up, hitInfo.normal, characterController2D.transform.forward);
                float wallAngle       = Mathf.Abs(wallSignedAngle);

                if (Utilities.isCloseTo(wallAngle, 90f, 0.1f))
                {
                    characterController2D.Teleport(hitInfo.point, Quaternion.LookRotation(Vector3.forward, hitInfo.normal));

                    if (OnWallAlignmentPerformed != null)
                    {
                        OnWallAlignmentPerformed();
                    }
                }
            }
        }