public void CollideAndSlide(ref Vector3 position, Vector3 displacement, bool useFullBody)
        {
            Vector3 groundPlaneNormal  = GroundStableNormal;
            Vector3 slidingPlaneNormal = Vector3.zero;

            HitInfoFilter filter = new HitInfoFilter(
                PhysicsComponent.CollisionLayerMask,
                false,
                true
                );

            int iteration = 0;


            while (iteration < CharacterConstants.MaxSlideIterations)
            {
                iteration++;

                CollisionInfo collisionInfo;
                bool          hit = characterCollisions.CastBody(
                    out collisionInfo,
                    position,
                    displacement,
                    useFullBody ? 0f : StepOffset,
                    filter
                    );


                if (hit)
                {
                    //---
                    if (canPushDynamicRigidbodies)
                    {
                        if (collisionInfo.hitInfo.IsRigidbody)
                        {
                            if (!collisionInfo.hitInfo.IsKinematicRigidbody)
                            {
                                bool canPushThisObject = CustomUtilities.BelongsToLayerMask(collisionInfo.hitInfo.transform.gameObject.layer, pushableRigidbodyLayerMask);
                                if (canPushThisObject)
                                {
                                    // Use the entire displacement and stop the collide and slide
                                    position += displacement;
                                    break;
                                }
                            }
                        }
                    }


                    if (slideOnWalls)
                    {
                        position     += collisionInfo.displacement;
                        displacement -= collisionInfo.displacement;

                        // Get the new slide plane
                        bool blocked = UpdateSlidingPlanes(
                            collisionInfo,
                            ref slidingPlaneNormal,
                            ref groundPlaneNormal,
                            ref displacement
                            );
                    }
                    else
                    {
                        if (!WallCollision)
                        {
                            position += collisionInfo.displacement;
                        }

                        break;
                    }
                }
                else
                {
                    position += displacement;
                    break;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Apply the jump velocity to the vertical velocity vector, based on the current jump state.
        /// </summary>
        void ProcessJump(float dt)
        {
            bool coyoteTimeValid = !CharacterActor.IsGrounded && CharacterActor.NotGroundedTime <= verticalMovementParameters.coyoteTime && groundedJumpAvailable;

            if (CharacterActor.IsGrounded)
            {
                notGroundedJumpsLeft = verticalMovementParameters.availableNotGroundedJumps;
            }


            groundedJumpAvailable = CharacterActor.IsGrounded || coyoteTimeValid;


            if (!verticalMovementParameters.canJumpOnUnstableGround && CharacterActor.CurrentState == CharacterActorState.UnstableGrounded)
            {
                groundedJumpAvailable = false;
            }

            if (isJumping)
            {
                CharacterActor.VerticalVelocity = jumpVelocity;
                jumpTimer += dt;

                if (jumpTimer > verticalMovementParameters.constantJumpDuration || CharacterActions.jump.Canceled)
                {
                    isJumping    = false;
                    jumpVelocity = Vector3.zero;
                    jumpTimer    = 0f;
                }
            }
            else
            {
                // Calculate the jump velocity vector ------------------------------------------------------
                if (CharacterActions.jump.Started && verticalMovementParameters.canJump)
                {
                    // Jump Down -------------------------------------------------
                    if (
                        CharacterActor.IsGrounded &&
                        CharacterActions.crouch.value &&
                        CustomUtilities.BelongsToLayerMask(CharacterActor.GroundObject.layer, CharacterActor.TagsAndLayersProfile.oneWayPlatforms)
                        )
                    {
                        CharacterActor.Velocity = -CharacterActor.Up * 0.5f;
                        CharacterActor.ForceNotGrounded();
                        CharacterActor.Position -= CharacterActor.Up * 0.1f;
                    }
                    else if (groundedJumpAvailable)
                    {
                        groundedJumpAvailable = false;

                        SetJumpVelocity();
                        CharacterActor.ForceNotGrounded();

                        if (verticalMovementParameters.jumpReleaseAction == VerticalMovementParameters.JumpReleaseAction.StopJumping)
                        {
                            isJumping = true;
                            jumpTimer = 0f;
                        }



                        CharacterActor.VerticalVelocity = jumpVelocity;
                    }
                    else
                    {
                        if (notGroundedJumpsLeft != 0)
                        {
                            SetJumpVelocity();

                            notGroundedJumpsLeft--;

                            if (verticalMovementParameters.jumpReleaseAction == VerticalMovementParameters.JumpReleaseAction.StopJumping)
                            {
                                isJumping = true;
                                jumpTimer = 0f;
                            }

                            CharacterActor.VerticalVelocity = jumpVelocity;
                        }
                    }
                }
            }
        }