Ejemplo n.º 1
0
        /*private void Update()
         * {
         *  if (m_Animator.layerCount > 1) {
         *      // Useful for debugging synchronization problems:
         *      var baseLayer = m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
         *      baseLayer -= (int)baseLayer;
         *      var upperLayer = m_Animator.GetCurrentAnimatorStateInfo(1).normalizedTime;
         *      upperLayer -= (int)upperLayer;
         *      Debug.Log("Base Time: " + baseLayer + " Upper Time: " + upperLayer + " Diff: " + (baseLayer - upperLayer));
         *  }
         * }*/

        /// <summary>
        /// Change the Animator layer to the specified state with the desired transition.
        /// </summary>
        /// <param name="layer">The layer to change the state on.</param>
        /// <param name="animatorStateData">The data about the destination state.</param>
        /// <param name="normalizedTime">The normalized time to start playing the animation state.</param>
        /// <returns>True if the state was changed.</returns>
        private bool ChangeAnimatorStates(int layer, AnimatorStateData animatorStateData, float normalizedTime)
        {
            // Animator state data may be null if there is no inventory and an item state is trying to be changed.
            if (animatorStateData == null)
            {
                return(false);
            }

            // The destination state depends on the equipped item and whether or not that item specifies a lower body state name. Format the lower and upper
            // states to take the item into account.
            var destinationState = string.Empty;

            if (animatorStateData is AnimatorItemStateData)
            {
                var animatorItemStateData = animatorStateData as AnimatorItemStateData;
                destinationState = FormatStateName(animatorItemStateData, layer);
                // Allow item states change the root motion state. This will allow the character to stop moving when a state should be still.
                if (layer == BaseLayerIndex && !m_Controller.UseRootMotion)
                {
                    m_Controller.ForceItemRootMotion = animatorItemStateData.ForceRootMotion;
                }
            }
            else
            {
                if (layer == BaseLayerIndex && m_Controller.ForceItemRootMotion)
                {
                    m_Controller.ForceItemRootMotion = m_Controller.UseRootMotion;
                }
                destinationState = animatorStateData.Name;
            }

            return(ChangeAnimatorStates(layer, destinationState, animatorStateData.TransitionDuration, animatorStateData.CanReplay, animatorStateData.SpeedMultiplier, normalizedTime));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determine the state that the specified layer should be in.
        /// </summary>
        /// <param name="layer">The layer to determine the state of.</param>
        /// <param name="defaultState">The default state to be in if no other states should run.</param>
        /// <param name="checkAbilities">Should the abilities be checked to determine if they have control?</param>
        /// <param name="baseStart">Is the base layer being set at the same time?</param>
        /// <returns>True if the state was changed.</returns>
        public virtual bool DetermineState(int layer, AnimatorStateData defaultState, bool checkAbilities, bool baseStart)
        {
            // Return early if there is no layer at the specified layer index.
            if (layer > m_Animator.layerCount - 1)
            {
                return(false);
            }

            // Try to play a high or medium priority item state. Abilities have the chance to override either of these states.
            var allowStateChange = true;

            for (int i = 0; i < m_Controller.Abilities.Length; ++i)
            {
                if (m_Controller.Abilities[i].IsActive && m_Controller.Abilities[i].HasAnimatorControl() && m_Controller.Abilities[i].OverrideItemState(layer))
                {
                    allowStateChange = false;
                    break;
                }
            }
            bool stateChange;
            AnimatorItemStateData state;

            if (allowStateChange)
            {
                if ((state = HasItemState(Item.ItemAnimationPriority.High, layer, 0, false, out stateChange)) != null)
                {
                    return(ChangeItemState(state, layer, 0));
                }
                if ((state = HasItemState(Item.ItemAnimationPriority.Medium, layer, 0, false, out stateChange)) != null)
                {
                    return(ChangeItemState(state, layer, 0));
                }
            }

            // Synchronize with the base layer.
            var normalizedTime = 0f;

            if (!baseStart)
            {
                var baseLayer = BaseLayerIndex;
                if (m_Animator.IsInTransition(baseLayer))
                {
                    normalizedTime = m_Animator.GetNextAnimatorStateInfo(baseLayer).normalizedTime % 1;
                }
                else
                {
                    normalizedTime = m_Animator.GetCurrentAnimatorStateInfo(baseLayer).normalizedTime % 1;
                }
            }

            for (int i = 0; i < m_Controller.Abilities.Length; ++i)
            {
                if (m_Controller.Abilities[i].IsActive && m_Controller.Abilities[i].HasAnimatorControl())
                {
                    if (!m_Controller.Abilities[i].AllowStateTransitions(layer))
                    {
                        return(false);
                    }
                    var destinationState = m_Controller.Abilities[i].GetDestinationState(layer);
                    if (!string.IsNullOrEmpty(destinationState))
                    {
                        // Give the item state one more chance to play if an ability state should play.
                        if ((state = HasItemState(Item.ItemAnimationPriority.Low, layer, normalizedTime, false, out stateChange)) != null)
                        {
                            // The ability has to match in order for the item state to override the ability state.
                            if (!m_IgnoreLowerPriority && state.Ability == m_Controller.Abilities[i])
                            {
                                return(ChangeItemState(state, layer, 0));
                            }
                        }
                        return(ChangeAnimatorStates(layer, destinationState, m_Controller.Abilities[i].GetTransitionDuration(), m_Controller.Abilities[i].CanReplayAnimationStates(),
                                                    m_Controller.Abilities[i].SpeedMultiplier, m_Controller.Abilities[i].GetNormalizedTime()));
                    }
                    if (!m_Controller.Abilities[i].IsConcurrentAbility())
                    {
                        break;
                    }
                }
            }
            if (!m_IgnoreLowerPriority && (state = HasItemState(Item.ItemAnimationPriority.Low, layer, normalizedTime, false, out stateChange)) != null)
            {
                return(ChangeItemState(state, layer, normalizedTime));
            }
            return(ChangeAnimatorStates(layer, defaultState, 0));
        }