Beispiel #1
0
    void Update()
    {
        if (CanPause && Input.GetButtonDown("Menu"))
        {
            PauseMenu.Instance.Display();
        }

        FullscreenMap.Instance.gameObject.SetActive(Input.GetButton("Map"));

        bool wasGrounded     = m_Grounded;
        bool loosedGrounding = false;

        //we define our own grounded and not use the Character controller one as the character controller can flicker
        //between grounded/not grounded on small step and the like. So we actually make the controller "not grounded" only
        //if the character controller reported not being grounded for at least .5 second;
        if (!m_CharacterController.isGrounded)
        {
            if (m_Grounded)
            {
                m_GroundedTimer += Time.deltaTime;
                if (m_GroundedTimer >= 0.5f)
                {
                    loosedGrounding = true;
                    m_Grounded      = false;
                }
            }
        }
        else
        {
            m_GroundedTimer = 0.0f;
            m_Grounded      = true;
        }

        Speed = 0;
        Vector3 move = Vector3.zero;

        if (!m_IsPaused && !LockControl)
        {
            // Jump (we do it first as
            if (m_Grounded && Input.GetButtonDown("Jump"))
            {
                m_VerticalSpeed = JumpSpeed;
                m_Grounded      = false;
                loosedGrounding = true;
                FootstepPlayer.PlayClip(JumpingAudioCLip, 0.8f, 1.1f);
            }

            bool  running     = m_Weapons[m_CurrentWeapon].CurrentState == Weapon.WeaponState.Idle && Input.GetButton("Run");
            float actualSpeed = running ? RunningSpeed : PlayerSpeed;

            if (loosedGrounding)
            {
                m_SpeedAtJump = actualSpeed;
            }

            // Move around with WASD
            move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
            if (move.sqrMagnitude > 1.0f)
            {
                move.Normalize();
            }

            float usedSpeed = m_Grounded ? actualSpeed : m_SpeedAtJump;

            move = move * usedSpeed * Time.deltaTime;

            move = transform.TransformDirection(move);
            m_CharacterController.Move(move);

            // Turn player
            float turnPlayer = Input.GetAxis("Mouse X") * MouseSensitivity;
            m_HorizontalAngle = m_HorizontalAngle + turnPlayer;

            if (m_HorizontalAngle > 360)
            {
                m_HorizontalAngle -= 360.0f;
            }
            if (m_HorizontalAngle < 0)
            {
                m_HorizontalAngle += 360.0f;
            }

            Vector3 currentAngles = transform.localEulerAngles;
            currentAngles.y            = m_HorizontalAngle;
            transform.localEulerAngles = currentAngles;

            // Camera look up/down
            var turnCam = -Input.GetAxis("Mouse Y");
            turnCam         = turnCam * MouseSensitivity;
            m_VerticalAngle = Mathf.Clamp(turnCam + m_VerticalAngle, -89.0f, 89.0f);
            currentAngles   = CameraPosition.transform.localEulerAngles;
            currentAngles.x = m_VerticalAngle;
            CameraPosition.transform.localEulerAngles = currentAngles;

            m_Weapons[m_CurrentWeapon].triggerDown = Input.GetMouseButton(0);

            Speed = move.magnitude / (PlayerSpeed * Time.deltaTime);

            if (Input.GetButton("Reload"))
            {
                m_Weapons[m_CurrentWeapon].Reload();
            }

            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                ChangeWeapon(m_CurrentWeapon - 1);
            }
            else if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                ChangeWeapon(m_CurrentWeapon + 1);
            }

            //Key input to change weapon

            for (int i = 0; i < 10; ++i)
            {
                if (Input.GetKeyDown(KeyCode.Alpha0 + i))
                {
                    int num = 0;
                    if (i == 0)
                    {
                        num = 10;
                    }
                    else
                    {
                        num = i - 1;
                    }

                    if (num < m_Weapons.Count)
                    {
                        ChangeWeapon(num);
                    }
                }
            }
        }

        // Fall down / gravity
        m_VerticalSpeed = m_VerticalSpeed - 10.0f * Time.deltaTime;
        if (m_VerticalSpeed < -10.0f)
        {
            m_VerticalSpeed = -10.0f; // max fall speed
        }
        var verticalMove = new Vector3(0, m_VerticalSpeed * Time.deltaTime, 0);
        var flag         = m_CharacterController.Move(verticalMove);

        if ((flag & CollisionFlags.Below) != 0)
        {
            m_VerticalSpeed = 0;
        }

        if (!wasGrounded && m_Grounded)
        {
            FootstepPlayer.PlayClip(LandingAudioClip, 0.8f, 1.1f);
        }
    }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        void Update()
        {
            bool wasGrounded     = m_Grounded;
            bool loosedGrounding = false;

            //we define our own grounded and not use the Character controller one as the character controller can flicker
            //between grounded/not grounded on small step and the like. So we actually make the controller "not grounded" only
            //if the character controller reported not being grounded for at least .5 second;
            if (!m_CharacterController.isGrounded)
            {
                if (m_Grounded)
                {
                    m_GroundedTimer += Time.deltaTime;
                    if (m_GroundedTimer >= 0.5f)
                    {
                        loosedGrounding = true;
                        m_Grounded      = false;
                    }
                }
            }
            else
            {
                m_GroundedTimer = 0.0f;
                m_Grounded      = true;
            }

            Speed = 0;
            Vector3 move = Vector3.zero;

            if (!m_IsPaused)
            {
                // Jump (we do it first as
                //if (m_Grounded && Input.GetButtonDown("Jump"))
                if (m_Grounded && bl_MobileInput.GetButtonDown("Jump") && !isCrounch)
                {
                    m_VerticalSpeed = JumpSpeed;
                    m_Grounded      = false;
                    loosedGrounding = true;
                    FootstepPlayer.PlayClip(JumpingAudioCLip, 0.8f, 1.1f);
                }

                // bool running = weaponManager.CurrentWeapon.CurrentState == Weapon.WeaponState.Idle && Input.GetButton("Run");
                bool  running     = weaponManager.CurrentWeapon.CurrentState == Weapon.WeaponState.Idle && bl_MovementJoystick.Instance.isRunning;
                float actualSpeed = running ? RunningSpeed : PlayerSpeed;

                CrounchController();
                AimControl();

                if (loosedGrounding)
                {
                    m_SpeedAtJump = actualSpeed;
                }

                // Move around with WASD
                // move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
                move = new Vector3(bl_MovementJoystick.Instance.Horizontal, 0, bl_MovementJoystick.Instance.Vertical);
                if (move.sqrMagnitude > 1.0f)
                {
                    move.Normalize();
                }

                float usedSpeed = m_Grounded ? actualSpeed : m_SpeedAtJump;

                move = move * usedSpeed * Time.deltaTime;

                move = transform.TransformDirection(move);
                m_CharacterController.Move(move);

                // Turn player
                // float turnPlayer =  Input.GetAxis("Mouse X") * MouseSensitivity;
                Vector2 mouseInput = bl_TouchPad.GetInputSmooth();
                float   turnPlayer = mouseInput.x * MouseSensitivity;
                m_HorizontalAngle = m_HorizontalAngle + turnPlayer;

                if (m_HorizontalAngle > 360)
                {
                    m_HorizontalAngle -= 360.0f;
                }
                if (m_HorizontalAngle < 0)
                {
                    m_HorizontalAngle += 360.0f;
                }

                Vector3 currentAngles = transform.localEulerAngles;
                currentAngles.y            = m_HorizontalAngle;
                transform.localEulerAngles = currentAngles;

                // Camera look up/down
                //var turnCam = -Input.GetAxis("Mouse Y");
                var turnCam = -mouseInput.y;
                turnCam         = turnCam * MouseSensitivity;
                m_VerticalAngle = Mathf.Clamp(turnCam + m_VerticalAngle, -89.0f, 89.0f);
                currentAngles   = CameraPosition.transform.localEulerAngles;
                currentAngles.x = m_VerticalAngle;
                CameraPosition.transform.localEulerAngles = currentAngles;
                Speed = move.magnitude / (PlayerSpeed * Time.deltaTime);
            }

            // Fall down / gravity
            m_VerticalSpeed = m_VerticalSpeed - 10.0f * Time.deltaTime;
            if (m_VerticalSpeed < -10.0f)
            {
                m_VerticalSpeed = -10.0f; // max fall speed
            }
            var verticalMove = new Vector3(0, m_VerticalSpeed * Time.deltaTime, 0);
            var flag         = m_CharacterController.Move(verticalMove);

            if ((flag & CollisionFlags.Below) != 0)
            {
                m_VerticalSpeed = 0;
            }

            if (!wasGrounded && m_Grounded)
            {
                FootstepPlayer.PlayClip(LandingAudioClip, 0.8f, 1.1f);
            }
        }