예제 #1
0
        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);
        }
예제 #2
0
        private void FootStepUpdate(float _movementSpeed)
        {
            float _speedThreshold = 0.05f;

            if (useAnimationBasedFootsteps)
            {
                //Get current foot step value from animator;
                float _newFootStepValue = animator.GetFloat("FootStep");

                //Play a foot step audio clip whenever the foot step value changes its sign;
                if ((currentFootStepValue <= 0f && _newFootStepValue > 0f) || (currentFootStepValue >= 0f && _newFootStepValue < 0f))
                {
                    //Only play footstep sound if mover is grounded and movement speed is above the threshold;
                    if (mover.IsGrounded() && _movementSpeed > _speedThreshold)
                    {
                        PlayFootstepSound(_movementSpeed);
                    }
                }
                currentFootStepValue = _newFootStepValue;
            }
            else
            {
                currentFootstepDistance += Time.deltaTime * _movementSpeed;

                //Play foot step audio clip if a certain distance has been traveled;
                if (currentFootstepDistance > footstepDistance)
                {
                    //Only play footstep sound if mover is grounded and movement speed is above the threshold;
                    if (mover.IsGrounded() && _movementSpeed > _speedThreshold)
                    {
                        PlayFootstepSound(_movementSpeed);
                    }
                    currentFootstepDistance = 0f;
                }
            }
        }
예제 #3
0
        //Determine current controller state based on current momentum and whether the controller is grounded (or not);
        //Handle state transitions;
        ControllerState DetermineControllerState()
        {
            //Check if vertical momentum is pointing upwards;
            bool _isRising = IsRisingOrFalling() && (VectorMath.GetDotProduct(GetMomentum(), tr.up) > 0f);
            //Check if controller is sliding;
            bool _isSliding = mover.IsGrounded() && IsGroundTooSteep();

            //Grounded;
            if (currentControllerState == ControllerState.Grounded)
            {
                if (_isRising)
                {
                    OnGroundContactLost();
                    return(ControllerState.Rising);
                }
                if (!mover.IsGrounded())
                {
                    OnGroundContactLost();
                    return(ControllerState.Falling);
                }
                if (_isSliding)
                {
                    return(ControllerState.Sliding);
                }
                return(ControllerState.Grounded);
            }

            //Falling;
            if (currentControllerState == ControllerState.Falling)
            {
                if (_isRising)
                {
                    return(ControllerState.Rising);
                }
                if (mover.IsGrounded() && !_isSliding)
                {
                    OnGroundContactRegained(momentum);
                    return(ControllerState.Grounded);
                }
                if (_isSliding)
                {
                    OnGroundContactRegained(momentum);
                    return(ControllerState.Sliding);
                }
                return(ControllerState.Falling);
            }

            //Sliding;
            if (currentControllerState == ControllerState.Sliding)
            {
                if (_isRising)
                {
                    OnGroundContactLost();
                    return(ControllerState.Rising);
                }
                if (!mover.IsGrounded())
                {
                    return(ControllerState.Falling);
                }
                if (mover.IsGrounded() && !_isSliding)
                {
                    OnGroundContactRegained(momentum);
                    return(ControllerState.Grounded);
                }
                return(ControllerState.Sliding);
            }

            //Rising;
            if (currentControllerState == ControllerState.Rising)
            {
                if (!_isRising)
                {
                    if (mover.IsGrounded() && !_isSliding)
                    {
                        OnGroundContactRegained(momentum);
                        return(ControllerState.Grounded);
                    }
                    if (_isSliding)
                    {
                        return(ControllerState.Sliding);
                    }
                    if (!mover.IsGrounded())
                    {
                        return(ControllerState.Falling);
                    }
                }

                //If a ceiling detector has been attached to this gameobject, check for ceiling hits;
                if (ceilingDetector != null)
                {
                    if (ceilingDetector.HitCeiling())
                    {
                        OnCeilingContact();
                        return(ControllerState.Falling);
                    }
                }
                return(ControllerState.Rising);
            }

            //Jumping;
            if (currentControllerState == ControllerState.Jumping)
            {
                //Check for jump timeout;
                if ((Time.time - currentJumpStartTime) > jumpDuration)
                {
                    return(ControllerState.Rising);
                }

                //Check if jump key was let go;
                if (jumpKeyWasLetGo)
                {
                    return(ControllerState.Rising);
                }

                //If a ceiling detector has been attached to this gameobject, check for ceiling hits;
                if (ceilingDetector != null)
                {
                    if (ceilingDetector.HitCeiling())
                    {
                        OnCeilingContact();
                        return(ControllerState.Falling);
                    }
                }
                return(ControllerState.Jumping);
            }

            return(ControllerState.Falling);
        }