示例#1
0
    // Update is called once per frame
    void Update()
    {
        // If the Character Controller is grounded
        if (controller.isGrounded)
        {
            // Set the vertical movement to zero
            currentVelocity.y = 0f;

            // Get the desired velocity from input
            desiredVelocity = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

            // Normalize the desired velocity when necessary
            float magnitude = Mathf.Abs(desiredVelocity.x) + Mathf.Abs(desiredVelocity.z);
            if (magnitude > 1f)
            {
                desiredVelocity /= magnitude;
            }

            // Apply speed multipliers to the desired velocity
            desiredVelocity *= speedMultiplier.GetMultiplier();
            if (Input.GetButton("Fire3"))
            {
                desiredVelocity *= 0.5f;
            }

            // Apply movement to the desired velocity (in world space)
            desiredVelocity  = transform.TransformDirection(desiredVelocity);
            desiredVelocity *= movementSpeed;

            // Lerp between the current velocity and the desired velocity
            currentVelocity = Vector3.Lerp(currentVelocity, desiredVelocity, smoothing);

            /*
             * // If the jump buttom was pressed
             * if (Input.GetButtonDown("Jump"))
             * {
             *  // Apply jumping to the current velocity
             *  currentVelocity.y = jumpSpeed;
             * }
             */
        }

        // Apply gravity to the current velocity
        currentVelocity.y -= gravity * Time.deltaTime;

        // Move the Character Controller based on the player's current velocity
        controller.Move(currentVelocity * Time.deltaTime);
    }