示例#1
0
        // Called every frame that this input script is active.
        protected override void InputUpdate()
        {
            // Get the movement input
            float forward = Input.GetAxis("Vertical");
            float right   = Input.GetAxis("Horizontal");

            // Get the target input vector
            Vector3 movementInputs = Vector3.ClampMagnitude(new Vector3(right, 0f, forward), 1);

            // Get the next input vector
            movementInputs     = Vector3.Lerp(lastMovementInputs, movementInputs, (1 / (1 + movementSmoothing)));
            lastMovementInputs = movementInputs;

            // Update whether the character is reversing
            bool reversing = movementInputs.z < -0.01f;

            m_RigidbodyCharacterController.SetReversing(reversing);

            // Convert the input to a local movement vector
            Vector3 worldMovementDirection;

            if (lookController != null)
            {
                worldMovementDirection = lookController.GimbalController.HorizontalPivot.TransformDirection(movementInputs).normalized;
            }
            else
            {
                worldMovementDirection = Camera.main.transform.TransformDirection(movementInputs).normalized;
            }

            Vector3 localMovementDirection = m_RigidbodyCharacterController.transform.InverseTransformDirection(worldMovementDirection);

            // Send movement inputs
            m_RigidbodyCharacterController.SetMovementInputs(localMovementDirection * movementInputs.magnitude);

            // Get the jump input
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (m_RigidbodyCharacterController.Grounded)
                {
                    m_RigidbodyCharacterController.Jump();
                }
                else
                {
                    m_RigidbodyCharacterController.ActivateJetpack();
                }
            }

            // Deactivate jetpack on release of input
            if (Input.GetKeyUp(KeyCode.Space) && m_RigidbodyCharacterController.Jetpacking)
            {
                m_RigidbodyCharacterController.DeactivateJetpack();
            }

            // Set whether the character is running
            m_RigidbodyCharacterController.SetRunning(Input.GetKey(KeyCode.LeftShift));

            // Rotate the character to face the movement vector
            float turnAmount = 0;

            if (movementInputs.magnitude > 0.01f)
            {
                Vector3 localLegsTargetDirection = localMovementDirection;

                if (reversing)
                {
                    localLegsTargetDirection *= -1;
                }

                turnAmount = Mathf.Atan2(localLegsTargetDirection.x, localLegsTargetDirection.z) * 10;
            }

            // Send rotation inputs to the character
            m_RigidbodyCharacterController.SetRotationInputs(new Vector3(0f, turnAmount, 0f));
        }