Esempio n. 1
0
        /// <summary>
        /// Accepts input and will perform an immediate action (such as crouching or jumping).
        /// </summary>
        private void Update()
        {
#if ENABLE_MULTIPLAYER
            if (!isLocalPlayer)
            {
                return;
            }
#endif
            if (!m_AllowGameplayInput)
            {
                m_HorizontalMovement = m_ForwardMovement = 0;
                m_LookRotation       = m_Transform.rotation;
#if ENABLE_MULTIPLAYER
                CmdSetInputParameters(m_HorizontalMovement, m_ForwardMovement, m_LookRotation);
#endif
                return;
            }

            m_HorizontalMovement = m_PlayerInput.GetAxisRaw(Constants.HorizontalInputName);
            m_ForwardMovement    = m_PlayerInput.GetAxisRaw(Constants.ForwardInputName);
            // Update the look rotation within update for top down and 2.5D movement types because the rotation depends on input rater than the camera.
            if (m_Controller.Movement == RigidbodyCharacterController.MovementType.TopDown || m_Controller.Movement == RigidbodyCharacterController.MovementType.Pseudo3D || m_Controller.IndependentLook())
            {
                if (m_Controller.LookInMoveDirection)
                {
                    var direction = Vector3.zero;
                    direction.x = m_HorizontalMovement;
                    direction.z = m_ForwardMovement;
                    if (direction.sqrMagnitude > 0.01f)
                    {
                        m_LookRotation = Quaternion.LookRotation(direction);
                    }
                    else
                    {
                        m_LookRotation = m_Transform.rotation;
                    }
                }
                else
                {
                    var mousePosition = (Vector3)m_PlayerInput.GetMousePosition();
                    if ((mousePosition - m_PrevMousePosition).sqrMagnitude > 0.1f && !m_Controller.IndependentLook())
                    {
                        var        ray = m_Camera.ScreenPointToRay(mousePosition);
                        RaycastHit hit;
                        if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerManager.Mask.IgnoreInvisibleLayersPlayer, QueryTriggerInteraction.Ignore))
                        {
                            var hitPosition = hit.point;
                            hitPosition.y  = m_Transform.position.y;
                            m_LookRotation = Quaternion.LookRotation(hitPosition - transform.position);
                        }
                        else
                        {
                            var direction = mousePosition - m_Camera.WorldToScreenPoint(m_Transform.position + m_Controller.CapsuleCollider.center);
                            // Convert the XY direction to an XYZ direction with Y equal to 0.
                            direction.z    = direction.y;
                            direction.y    = 0;
                            m_LookRotation = Quaternion.LookRotation(direction);
                        }
                    }
                    else
                    {
                        var direction = Vector3.zero;
                        direction.x = m_PlayerInput.GetAxisRaw(Constants.YawInputName);
                        direction.z = m_PlayerInput.GetAxisRaw(Constants.PitchInputName);
                        if (direction.sqrMagnitude > 0.1f)
                        {
                            m_LookRotation = Quaternion.LookRotation(direction);
                        }
                        else
                        {
                            m_LookRotation = m_Transform.rotation;
                        }
                    }
                    m_PrevMousePosition = mousePosition;
                }
            }
            else if (m_Controller.Movement == RigidbodyCharacterController.MovementType.RPG)
            {
                if (m_PlayerInput.GetButton(Constants.SecondaryDisableButtonName) && m_PlayerInput.GetButton(Constants.PrimaryDisableButtonName))
                {
                    m_ForwardMovement = 1;
                }
                else if (!m_PlayerInput.GetButton(Constants.PrimaryDisableButtonName))
                {
                    m_HorizontalMovement = 0;
                }
            }
            else if (m_Controller.Movement == RigidbodyCharacterController.MovementType.FourLegged)
            {
                // Four legged characters should turn reverse of the input when moving backwards.
                if (m_ForwardMovement < -0.01f)
                {
                    m_HorizontalMovement *= -1;
                }
            }
#if ENABLE_MULTIPLAYER
            CmdSetInputParameters(m_HorizontalMovement, m_ForwardMovement, m_LookRotation);
#endif

            // Should the controller aim?
            if (m_AimType == AimType.Down)
            {
                if (m_PlayerInput.GetButtonDown(Constants.AimInputName))
                {
                    m_Controller.Aim = true;
                }
                else if (m_Controller.Aim && !m_PlayerInput.GetButton(Constants.AimInputName))
                {
                    m_Controller.Aim = false;
                }
            }
            else if (m_AimType == AimType.Toggle)
            {
                if (m_PlayerInput.GetButtonDown(Constants.AimInputName))
                {
                    m_Controller.Aim = !m_Controller.Aiming;
                }
            }

            // Abilities can have their own input.
            if (m_AbilityInputName != null)
            {
                for (int i = 0; i < m_AbilityInputName.Count; ++i)
                {
                    if (m_PlayerInput.GetButtonDown(m_AbilityInputName[i]))
                    {
                        if (!string.IsNullOrEmpty(m_AbilityDownInputEvent[i]))
                        {
#if ENABLE_MULTIPLAYER
                            CmdExecuteAbilityEvent(m_AbilityDownInputEvent[i]);
                            if (isLocalPlayer)
                            {
#endif
                            EventHandler.ExecuteEvent(m_GameObject, m_AbilityDownInputEvent[i]);
#if ENABLE_MULTIPLAYER
                        }
#endif
                        }
                    }
                    else if (m_PlayerInput.GetButtonUp(m_AbilityInputName[i]))
                    {
#if ENABLE_MULTIPLAYER
                        CmdExecuteAbilityEvent(m_AbilityUpInputEvent[i]);
                        if (isLocalPlayer)
                        {
#endif
                        EventHandler.ExecuteEvent(m_GameObject, m_AbilityUpInputEvent[i]);
#if ENABLE_MULTIPLAYER
                    }
#endif
                    }
                }
            }

            // Start or stop the abilities.
            if (m_Controller.Abilities != null)
            {
                for (int i = 0; i < m_Controller.Abilities.Length; ++i)
                {
                    if ((m_Controller.Abilities[i].StartType == Ability.AbilityStartType.ButtonDown || m_Controller.Abilities[i].StopType == Ability.AbilityStopType.ButtonToggle) &&
                        m_PlayerInput.GetButtonDown(m_Controller.Abilities[i].InputName))
                    {
                        // Start the ability if it is not started and can be started when a button is down. Stop the ability if it is already started and
                        // the stop type is button toggle. A toggled button means the button has to be pressed and released before the ability can be stopped.
                        if (!m_Controller.Abilities[i].IsActive && m_Controller.Abilities[i].StartType == Ability.AbilityStartType.ButtonDown)
                        {
                            TryStartAbility(i);
                        }
                        else if (m_Controller.Abilities[i].StopType == Ability.AbilityStopType.ButtonToggle)
                        {
                            TryStopAbility(i);
                        }
                    }
                    else if (m_Controller.Abilities[i].StopType == Ability.AbilityStopType.ButtonUp && m_PlayerInput.GetButtonUp(m_Controller.Abilities[i].InputName))
                    {
                        // Stop the ability if the ability can be stopped when the button is up.
                        TryStopAbility(i);
                    }
                    else if (m_Controller.Abilities[i].StartType == Ability.AbilityStartType.DoublePress && m_PlayerInput.GetDoublePress(m_Controller.Abilities[i].InputName))
                    {
                        // Start the ability if the ability should be started with a double press.
                        if (!m_Controller.Abilities[i].IsActive)
                        {
                            TryStartAbility(i);
                        }
                    }
                }
            }
        }