示例#1
0
        /// <summary>
        /// Unity Update method
        /// Update is called every frame, if the MonoBehaviour is enabled
        /// </summary>
        void Update()
        {
#if DEBUG_INFO
            if (!m_Initialized)
            {
                Debug.LogError("Component not initialized! " + " < " + this.ToString() + ">");
                return;
            }
#endif

            bool strafeToggle = Input.GetKeyDown(KeyCode.H);
            if (strafeToggle)
            {
                m_Player.strafing = !m_Player.strafing;
            }


            // using crouch flag for releasing from trigger
            if (Input.GetButtonDown("Crouch"))
            {
                m_Player.triggers.breakTrigger();
            }
            float h         = Input.GetAxis("Horizontal");
            float v         = Input.GetAxis("Vertical");
            bool  jump      = Input.GetButtonDown("Jump");
            bool  runToggle = Input.GetButton("WalkToggle");
            bool  dive      = Input.GetButtonDown("DiveRoll");
            bool  crouch    = Input.GetButton("Crouch");
            bool  use       = Input.GetButtonDown("Use");

            if (dive)
            {
                m_PrevIKmode   = m_Player.character.getIKMode();
                m_PrevMoveMode = m_Player.character.moveMode;
                m_PrevStrafing = m_Player.strafing;
            }

            m_Player.triggers.update(h, v, use, false, jump);
            m_Player.control(h, v, jump, runToggle, dive, crouch);
        }
        /// <summary>
        /// control player by input
        /// </summary>
        protected override void _controlPlayer()
        {
#if DEBUG_INFO
            if (!m_Initialized)
            {
                Debug.LogError("Component not initialized < " + this.ToString() + " >");
                return;
            }
#endif

            m_Animator.SetFloat(HashIDs.AttackSpeedFloat /*"pAttackSpeed"*/, m_Stats.currentAttackSpeed);
            bool onLedge = m_Animator.GetBool(/*"pOnLedge"*/ HashIDs.OnLedgeBool);

            if (m_PrimaryTargetNPC != null) //有攻击目标
            {
                m_Direction2target   = m_PrimaryTargetNPC.position - transform.position;
                m_Direction2target.y = 0.0f;
                m_Direction2target.Normalize();
            }

            // check if secondary weapon is drawn but primary is not.
            // if so - draw primary  双手双武器  currentWeapon1H第一武器 currentSecondary第二武器
            //双手双武器就是两个单手武器,都在腿上
            if (m_CurrentWeaponMode == WeaponMode.DualWield)
            {
                if (m_EquipmentScript.currentWeapon1H &&
                    m_EquipmentScript.currentSecondary)
                {
                    if (!m_EquipmentScript.weaponInHand1H &&
                        m_EquipmentScript.secondaryWeaponInHand)
                    //这里如果第二武器在身上,并且第一武器没有装备
                    {
                        if (!m_SwitchingItemState)
                        {
#if ADDITIONAL_DEBUG_INFO
                            Debug.LogWarning("Secondary item alone eqipped - drawing primary.");
#endif
                            _startTakingWeapon1H(AnimClipSheet.DualWeapons);
                        }
                    }
                }
            }


            float   h             = 0.0f;
            float   v             = 0.0f;
            bool    dive          = false;
            bool    attack        = false;
            bool    block         = false;
            bool    jump          = false;
            bool    runToggle     = false;
            bool    crouch        = false;
            Vector3?bodyDirection = null;

            m_AttackStateUnderway  = m_Animator.GetCurrentAnimatorStateInfo(0).shortNameHash == HashIDs.UnarmedAttackComboState;
            m_AttackStateUnderway |= m_Animator.GetCurrentAnimatorStateInfo(0).shortNameHash == HashIDs.AttackComboState;

            if (!m_ThirdPersonPlayer.disableInput)
            {
                bool switchEquipmentCondition = !m_SwitchingItemState && !m_Attack_combo_started && !onLedge && !m_ThirdPersonPlayer.triggerActive;
                //是否允许切换武器
                bool attackBlockCondition = !m_SwitchingItemState && !m_ThirdPersonPlayer.triggerActive;
                //是否允许攻击格挡

                if (Input.GetButtonDown("ToggleWeapon") && switchEquipmentCondition)
                {
                    toggleCurrentWeapon(); //拔出当前武器或者收起当前武器
                }
                if (Input.GetButtonDown("DrawWeapon") && switchEquipmentCondition)
                {
                    takeCurrentWeapon();//这个100%是拔出武器
                }
                if (Input.GetButtonDown("SheatheWeapon") && switchEquipmentCondition)
                {
                    sheatheCurrentWeapon();//这个100%是收起武器
                }
                if (Input.GetButtonDown("Drop") && switchEquipmentCondition)
                {
                    dropAllEquipment();
                }
                //下面的切换模式不会拔出武器,也不会收起武器
                if (Input.GetButtonDown("WeaponShieldMode") && switchEquipmentCondition)
                {
                    switchWeaponMode(WeaponMode.Weapon1HShield);
                }
                if (Input.GetButtonDown("Weapon2HMode") && switchEquipmentCondition)
                {
                    switchWeaponMode(WeaponMode.Weapon2H);
                }
                if (Input.GetButtonDown("BowMode") && switchEquipmentCondition)
                {
                    switchWeaponMode(WeaponMode.Bow);
                }
                if (Input.GetButtonDown("DualWieldMode") && switchEquipmentCondition)
                {
                    switchWeaponMode(WeaponMode.DualWield);
                }

                if (!onLedge)
                {
                    // if taking hit
                    // allow dive only after halfish of taking hit animation is passed
                    bool halfHitAnimationPassed = true; //被击中动画已经播放一半以上
                    if (takingHit)
                    {
                        AnimatorStateInfo asi      = m_Animator.GetCurrentAnimatorStateInfo(0);
                        float             normTime = asi.normalizedTime;
                        halfHitAnimationPassed = normTime > 0.6f;
                    }
                    dive   = Input.GetButtonDown("DiveRoll") && !(m_Character.moveMode == TPCharacter.MovingMode.Ledge) && halfHitAnimationPassed && !m_SwitchingItemState;
                    attack = Input.GetButtonDown("Fire1") && attackBlockCondition;
                    block  = Input.GetButton("Fire2") && attackBlockCondition;
                }

                h = Input.GetAxisRaw("Horizontal");
                v = Input.GetAxisRaw("Vertical");

                Vector3 move     = Vector3.zero;
                Vector3 forwDir  = transform.forward;
                Vector3 rightDir = transform.right;
                if (m_ThirdPersonPlayer.m_Camera.transform != null)
                {
                    //那个方向是前方,往前移动是往哪个方向移动
                    forwDir = m_ThirdPersonPlayer.m_Camera.transform.forward;
                    //那个方向是右方
                    rightDir = m_ThirdPersonPlayer.m_Camera.transform.right;
                }
                move = v * forwDir + h * rightDir;
                //WASD移动前后左右 是针对摄像机,

                if (move == Vector3.zero)
                {
                    move           = transform.forward;
                    m_ChangeTarget = false;
                }
                move.y = 0.0f;

                m_TargetDirection = move.normalized; //当前帧往哪个方向移动
                float angleDiff = Vector3.Angle(m_PrevTargetDirection, m_TargetDirection);
                if (angleDiff > CHANGE_TARGET_ANGLE_BUFFER)
                {
                    m_ChangeTarget = true;
                }
                m_PrevTargetDirection = m_TargetDirection; //上一帧要往哪个方向移动


                bool blocking = false;
                if (!dive && !m_Character.isDiving)//没有按下dive滚动键,也没有正在滚动状态
                {
                    m_Animator.SetBool(HashIDs.BlockBool /*"pBlock"*/, false);
                    if (block)//按下了block按键
                    {
                        if (m_Character.isOnGround)
                        {
                            if (m_Attack_combo_started)
                            {
                                _attack_combo_end();
                            }
                            blocking = true;
                            if (m_PrimaryTargetNPC != null)
                            {
                                bodyDirection = m_Direction2target;
                            }
                        }
                    }

                    if (!blocking && !takingHit)
                    {
                        m_Animator.SetBool(HashIDs.BlockBool /*"pBlock"*/, false);
                        _attackCombo(attack && !m_SwitchingItemState);
                    }
                    else
                    {
                        m_Animator.SetBool(HashIDs.BlockBool /*"pBlock"*/, blocking);
                        m_Animator.SetBool(/*"pAttack1"*/ HashIDs.Attack1Bool, false);
                        m_BreakCombo           = true;  //终端连击计数器
                        m_Attack_combo_started = false; //连击计数器没有开始
                    }
                }


                jump      = Input.GetButtonDown("Jump") && !takingHit;
                runToggle = Input.GetButton("WalkToggle");
                crouch    = Input.GetButton("Crouch");

                if (m_Attack_combo_started) //连击计数器开始,那么不可以通过WASD移动,而是
                {
                    h = 0;
                    v = 0;
                }
                if (blocking || takingHit)
                {
                    bodyDirection = transform.forward;
                }
            }

            // turn to npc if not changed by input
            if (!m_ChangeTarget)
            {
                if (m_PrimaryTargetNPC != null)
                {
                    if (!m_PrimaryTargetNPC.isDead)
                    {
                        bodyDirection = m_Direction2target;
                    }
                }
            }

            if (dive)
            {
                m_PrevIKmode   = m_Character.getIKMode();
                m_PrevMoveMode = m_Character.moveMode;
                _attack_combo_end();
            }

            m_Character.moveSpeedMultiplier = m_Stats.DefaultMoveSpeed;

            // setup trigger parameters
            bool enableTriggerUse = !block && !takingHit && !m_Attack_combo_started;
            bool use = Input.GetButton("Use") && enableTriggerUse;
            //操作

            // disable trigger use when picking item
            if (m_ItemPicker.highlighted)
            {
                m_ThirdPersonPlayer.triggers.disableUse = true;
            }
            m_ThirdPersonPlayer.triggers.update(h, v, use, false, jump, enableTriggerUse);
            //h摄像机的前方移动多少米,v摄像机的右方移动多少米
            //是否成功按下(有些状态下按下无效)jump,是否按下runToggle
            //是否成功按下滚动,crouch下蹲
            //bodyDirection 身体前方方向-----就是模型的前方方向。
            //hv格式化之后就是m_TargetDirection
            m_ThirdPersonPlayer.control(h, v, jump, runToggle, dive, crouch, bodyDirection, m_TargetDirection);
        }