private void ProcessCollisionInfo(CharacterCollisionFlag collisionInfo)
        {
            //Collision above the player.
            if (collisionInfo == CharacterCollisionFlag.Up)
            {
                //Reset the velocity and update the grounded state.
                currentGravityAmount = 0f;
                isGrounded           = false;
            }

            //Collision on the sides.
            else if (collisionInfo == CharacterCollisionFlag.Sides)
            {
                //Update the grounded state. It may look weird, but we have to set it to true because otherwise we cannot jump while we are moving against a wall.
                isGrounded = true;
            }

            //Collision under the player.
            else if (collisionInfo == CharacterCollisionFlag.Down)
            {
                //Reset the velocity and update the grounded state.
                currentGravityAmount = 0f;
                isGrounded           = true;
            }

            //No collision. Update the grounded state.
            else
            {
                isGrounded = false;
            }
        }
        private void Move(Vector2 inputDir)
        {
            //Get the target move speed.
            Vector2 targetMoveSpeed = new Vector2(moveSpeed * inputDir.x, moveSpeed * inputDir.y);

            //Interpolate the move speed to get smooth movement.
            currentMoveSpeed.x = MathEx.Lerp(currentMoveSpeed.x, targetMoveSpeed.x, Time.FrameDelta / 0.04f);
            currentMoveSpeed.y = MathEx.Lerp(currentMoveSpeed.y, targetMoveSpeed.y, Time.FrameDelta / 0.04f);

            //Apply gravity.
            currentGravityAmount -= gravity * Time.FrameDelta;

            //Jumping.
            if (Input.IsButtonDown(ButtonCode.Space) && isGrounded)
            {
                currentGravityAmount = MovementUtilities.CalculateJumpVelocity(gravity, jumpHeight);
            }

            //Calculate the move velocity.
            Vector3 moveVelocity = SceneObject.Forward * currentMoveSpeed.y + SceneObject.Right * currentMoveSpeed.x + Vector3.YAxis * currentGravityAmount;

            //Move the player and catch the collision flag.
            CharacterCollisionFlag collisionInfo = controller.Move(moveVelocity * Time.FrameDelta);

            //Pass the collision flag to the collision handler method, and invoke it.
            ProcessCollisionInfo(collisionInfo);
        }
Esempio n. 3
0
        /// <summary>
        /// Moves the controller in the specified direction by the specified amount, while interacting with surrounding
        /// geometry.Returns flags signaling where collision occurred after the movement.
        ///
        /// Does not account for gravity, you must apply it manually.
        /// </summary>
        /// <param name="position">Position to move the controller to, in world space.</param>
        public CharacterCollisionFlag Move(Vector3 position)
        {
            if (native == null)
            {
                return(0);
            }

            CharacterCollisionFlag output = native.Move(position);

            UpdatePositionFromController();

            return(output);
        }