Beispiel #1
0
        //Update;
        void Update()
        {
            //Get controller velocity;
            Vector3 _velocity = controller.GetVelocity();

            //Split up velocity;
            Vector3 _horizontalVelocity = VectorMath.RemoveDotVector(_velocity, tr.up);
            Vector3 _verticalVelocity   = _velocity - _horizontalVelocity;

            //Smooth horizontal velocity for fluid animation;
            _horizontalVelocity = Vector3.Lerp(oldMovementVelocity, _horizontalVelocity, smoothingFactor);
            oldMovementVelocity = _horizontalVelocity;

            animator.SetFloat("VerticalSpeed", _verticalVelocity.magnitude * VectorMath.GetDotProduct(_verticalVelocity.normalized, tr.up));
            animator.SetFloat("HorizontalSpeed", _horizontalVelocity.magnitude);

            //If animator is strafing, split up horizontal velocity;
            if (useStrafeAnimations)
            {
                Vector3 _localVelocity = animatorTransform.InverseTransformVector(_horizontalVelocity);
                animator.SetFloat("ForwardSpeed", _localVelocity.z);
                animator.SetFloat("StrafeSpeed", _localVelocity.x);
            }

            //Pass values to animator;
            animator.SetBool("IsGrounded", controller.IsGrounded());
            animator.SetBool("IsStrafing", useStrafeAnimations);
        }
Beispiel #2
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);
        }