示例#1
0
    private void PlayerMove(ref float unstabilityModify)
    {
        // Receive input
        float horizontalAxis = InputHandler.GetHorizontalAxis();                // Z - Movement
        float verticalAxis   = InputHandler.GetVerticalAxis();                  // X - Movement
        float viewRotX       = InputHandler.GetViewVerticalAxis();              // View - X Rotation
        float viewRotY       = InputHandler.GetViewHorizontalAxis();            // View - Y Rotation

        // First we assert the player is not running. This will be changed in next context.
        isSprinting = false;

        // Apply velocity and camera rotation
        Vector3 velocity = new Vector3(horizontalAxis, 0, verticalAxis);

        velocity.Normalize();                           // Clamp magnitude
        velocity = transform.TransformVector(velocity); // Local to world

        // Run
        float currentSpeed = walkSpeed;

        if (CanSprint())
        {
            // If running key is held, and the player is tying to move:
            if (InputHandler.SprintKeyHeld() && velocity != Vector3.zero)
            {
                currentSpeed = walkSpeed * sprintSpeedMultiplier;
                isSprinting  = true;
            }
        }
        // Do some animation
        //controller.IsRunning(isSprinting);

        // Apply motion
        // Detect available slope angle, and then move along it.
        Vector3 resultVector = velocity;

        if (!isAir && resultVector != Vector3.zero)
        {
            CapsuleCollider capsule     = GetComponentInChildren <CapsuleCollider>();
            Vector3         bottomPoint = transform.position + Vector3.down * (capsule.height * 0.5f + capsule.radius - 0.2f);
            float           moveAngle   = -characterController.slopeLimit;

            for (; moveAngle <= 0; moveAngle += 10)
            {
                resultVector = Vector3.Slerp(velocity, transform.up * -1, -moveAngle / 90);
                if (!Physics.Raycast(bottomPoint, resultVector, forwardDetectDistance, groundMask))
                {
                    break;
                }
            }
        }

        // Apply movement accordingly
        Vector3 resultVelocity = resultVector * currentSpeed;

        motor.ApplyFlatMotion(resultVelocity);                                    // Scale magnitude
        motor.ApplyInputRotation(Quaternion.Euler(0, viewRotY, 0));               // Restricted Rotate RB
        motor.ApplyBasicCameraRotation(Quaternion.Euler(-viewRotX, viewRotY, 0)); // Free Rotate cam

        // Add unstability to weapon
        unstabilityModify += resultVelocity.magnitude * 10;

        if (resultVector != Vector3.zero)
        {
            // Handle walking noise
            MakeNoise(currentSpeed / (sprintSpeedMultiplier * walkSpeed) * 30f);
            // Remember inerita
            inertia        = resultVector;
            lastMotionTime = Time.time;
        }
    }