Exemple #1
0
        //FixedUpdate;
        void FixedUpdate()
        {
            //Check if mover is grounded;
            mover.CheckForGround();

            //Determine controller state;
            currentControllerState = DetermineControllerState();

            //Apply friction and gravity to 'momentum';
            HandleMomentum();

            //Check if the player has initiated a jump;
            HandleJumping();

            //Calculate movement velocity;
            Vector3 _velocity = CalculateMovementVelocity();

            //If local momentum is used, transform momentum into world space first;
            Vector3 _worldMomentum = momentum;

            if (useLocalMomentum)
            {
                _worldMomentum = tr.localToWorldMatrix * momentum;
            }

            //Add current momentum to velocity;
            _velocity += _worldMomentum;

            //If player is grounded or sliding on a slope, extend mover's sensor range;
            //This enables the player to walk up/down stairs and slopes without losing ground contact;
            mover.SetExtendSensorRange(IsGrounded());

            //Set mover velocity;
            mover.SetVelocity(_velocity);

            //Store velocity for next frame;
            savedVelocity         = _velocity;
            savedMovementVelocity = _velocity - _worldMomentum;

            //Reset jump key booleans;
            jumpKeyWasLetGo   = false;
            jumpKeyWasPressed = false;

            //Reset ceiling detector, if one was attached to this gameobject;
            if (ceilingDetector != null)
            {
                ceilingDetector.ResetFlags();
            }
        }
        void FixedUpdate()
        {
            //Run initial mover ground check;
            mover.CheckForGround();

            //If character was not grounded int the last frame and is now grounded, call 'OnGroundContactRegained' function;
            if (isGrounded == false && mover.IsGrounded() == true)
            {
                OnGroundContactRegained(lastVelocity);
            }

            //Check whether the character is grounded and store result;
            isGrounded = mover.IsGrounded();

            Vector3 _velocity = Vector3.zero;

            //Add player movement to velocity;
            _velocity += CalculateMovementDirection() * movementSpeed;

            //Handle gravity;
            if (!isGrounded)
            {
                currentVerticalSpeed -= gravity * Time.deltaTime;
            }
            else
            {
                if (currentVerticalSpeed <= 0f)
                {
                    currentVerticalSpeed = 0f;
                }
            }

            //Handle jumping;
            if ((characterInput != null) && isGrounded && characterInput.IsJumpKeyPressed())
            {
                OnJumpStart();
                currentVerticalSpeed = jumpSpeed;
                isGrounded           = false;
            }

            //Add vertical velocity;
            _velocity += tr.up * currentVerticalSpeed;

            //Save current velocity for next frame;
            lastVelocity = _velocity;

            mover.SetExtendSensorRange(isGrounded);
            mover.SetVelocity(_velocity);
        }