Esempio n. 1
0
    /**
     * Translates the player
     *@param axis - translation axis, defined by the user inputs
     */
    void Translate(Vector2 axis)
    {
        // apply the translation
        m_TranslationController.UpdateTargetSpeed(axis, GetRunStatus());

        m_Running         = false;
        m_BackwardRunning = false;
        m_WalkingLeft     = false;
        m_WalkingRight    = false;
        bool stillPlaying = false;

        // is the input value large enough to move the player, and the player is lying on the ground or can be controlled while jumping?
        if ((Mathf.Abs(axis.x) > float.Epsilon || Mathf.Abs(axis.y) > float.Epsilon) && (m_AirControl || IsGrounded))
        {
            // always move along the camera forward as it is the direction that it being aimed by the player
            Vector3 desiredMove = Camera.transform.forward * axis.y + Camera.transform.right * axis.x;
            desiredMove = Vector3.ProjectOnPlane(desiredMove, m_GroundContactNormal).normalized;

            // calculate the desired move amplitude
            desiredMove.x *= m_TranslationController.m_CurrentTargetSpeed;
            desiredMove.y *= m_TranslationController.m_CurrentTargetSpeed;
            desiredMove.z *= m_TranslationController.m_CurrentTargetSpeed;

            // run the running animation
            if (IsGrounded)
            {
                // side walking
                if (axis.x > 0.0f)
                {
                    m_WalkingRight = true;

                    if (m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_DoKick &&
                        m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_WalkingRight)
                    {
                        m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_WalkingRight;
                    }
                }
                else
                if (axis.x < 0.0f)
                {
                    m_WalkingLeft = true;

                    if (m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_DoKick &&
                        m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_WalkingLeft)
                    {
                        m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_WalkingLeft;
                    }
                }

                // front or backward running
                if (axis.y > 0.0f)
                {
                    m_Running = true;

                    if (m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_DoKick &&
                        m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_Running)
                    {
                        m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_Running;
                    }
                }
                else
                if (axis.y < 0.0f)
                {
                    m_BackwardRunning = true;

                    if (m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_DoKick &&
                        m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_BackwardRunning)
                    {
                        m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_BackwardRunning;
                    }
                }
            }

            // do move the body?
            if (RigidBody.velocity.sqrMagnitude < (m_TranslationController.m_CurrentTargetSpeed * m_TranslationController.m_CurrentTargetSpeed))
            {
                // move the body by applying a force on it along the player direction
                RigidBody.AddForce(desiredMove * GetSlopeMultiplier(), ForceMode.Impulse);

                // play the footstep on concrete sound
                m_SoundController.Play((int)ISoundController.IEType.IE_T_FootSteps_Concrete);

                // keep the sound playing alive
                stillPlaying = true;
            }
        }

        // set animation to idle if no longer running
        if (m_ModelController.AnimState != IModelController.IEAnimState.IE_AS_DoKick &&
            ((!m_Running && m_ModelController.AnimState == IModelController.IEAnimState.IE_AS_Running) ||
             (!m_BackwardRunning && m_ModelController.AnimState == IModelController.IEAnimState.IE_AS_BackwardRunning) ||
             (!m_WalkingLeft && m_ModelController.AnimState == IModelController.IEAnimState.IE_AS_WalkingLeft) ||
             (!m_WalkingRight && m_ModelController.AnimState == IModelController.IEAnimState.IE_AS_WalkingRight)))
        {
            // check if grounded, in case the jump was not performed by the user (e.g when he falls or hit an obstacle)
            if (IsGrounded)
            {
                m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_Idle;
            }
            else
            {
                m_ModelController.AnimState = IModelController.IEAnimState.IE_AS_Jumping;
            }
        }

        // don't allow single file footsteps to run infinitely
        if (!stillPlaying)
        {
            m_SoundController.Stop((int)ISoundController.IEType.IE_T_FootSteps_Concrete);
        }
    }