// Update is called once per frame void Update() { //Exit if we are not connected to anything if (m_animator == null) { return; } float distance = Vector3.Distance(transform.position, m_lastPosition); m_lastPosition = transform.position; if (Time.deltaTime > 0f) { //Get damped speed - damping smooths out frame rate variations a little m_speed = Mathf.Lerp(m_speed, distance / Time.deltaTime, Time.deltaTime * (1f / m_speedDamping)); //Determine what state we should be in, and adjust animation multiplier if (m_speed > m_maxWalkSpeed) { m_animationState = PegasusConstants.PegasusAnimationState.Running; m_animationMultiplier = m_speed / m_runSpeed; } else if (m_speed > 0f) { m_animationState = PegasusConstants.PegasusAnimationState.Walking; m_animationMultiplier = m_speed / m_walkSpeed; } else { m_animationState = PegasusConstants.PegasusAnimationState.Idle; m_animationMultiplier = 1f; } } else { m_speed = 0f; m_animationState = PegasusConstants.PegasusAnimationState.Idle; m_animationMultiplier = 1f; } if (m_animator != null && m_speed != m_lastSpeed) { m_lastSpeed = m_speed; m_animator.SetInteger(m_animationStateHash, (int)m_animationState); m_animator.SetFloat(m_animationMultiplierHash, m_animationMultiplier); } }
/// <summary> /// Play an animation state /// </summary> /// <param name="newState">The new state</param> /// <param name="forceStateNow">Force an animation state update</param> private void PlayState(PegasusConstants.PegasusAnimationState newState, bool forceStateNow) { if (forceStateNow) { //Set variables m_animationState = newState; m_animator.SetInteger(m_animationStateHash, (int)m_animationState); //Force animation switch (m_animationState) { case PegasusConstants.PegasusAnimationState.Idle: m_animator.Play("Base Layer.Idle"); break; case PegasusConstants.PegasusAnimationState.Walking: m_animator.Play("Base Layer.Walk"); break; case PegasusConstants.PegasusAnimationState.Running: m_animator.Play("Base Layer.Run"); break; default: throw new ArgumentOutOfRangeException(); } } else if (m_animationState != newState) { //Set variables m_animationState = newState; m_animator.SetInteger(m_animationStateHash, (int)m_animationState); } //And update the animation speed multiplier m_animator.SetFloat(m_animationMultiplierHash, m_animationMultiplier); }