Пример #1
0
        private void FixedUpdate()
        {
            Vector3 desiredMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

            desiredMove.Normalize();

            if (useMobileInput && leftJoystick != null)
            {
                if (leftJoystick.IsDragging)
                {
                    Vector2 joystickOffset = leftJoystick.GetJoystickOffset();
                    desiredMove += new Vector3(joystickOffset.x, 0, joystickOffset.y);
                }
            }

            if (desiredMove.magnitude > 1.0f)
            {
                desiredMove.Normalize();
            }

            desiredMove = cameraYawPivot.TransformDirection(desiredMove);

            //playerRigidbody.AddForce(Physics.gravity, ForceMode.Acceleration);
            Vector3 velocity = playerRigidbody.velocity;

            velocity += Physics.gravity * Time.fixedDeltaTime;
            playerRigidbody.velocity = velocity;

            UpdateGrounding();

            float acceleration = isGrounded ? groundAcceleration : airAcceleration;

            if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.LeftShift))
            {
                acceleration *= runAccelerationScaler;
            }

            velocity  = playerRigidbody.velocity;
            velocity += desiredMove * acceleration * Time.fixedDeltaTime;

            float drag       = isGrounded ? groundDrag : airDrag;
            float dragFactor = 1.0f / (1.0f + drag * Time.fixedDeltaTime);

            velocity.x *= dragFactor;
            velocity.z *= dragFactor;

            playerRigidbody.velocity = velocity;

            FixedUpdatePickUpAndThrow();
        }
Пример #2
0
        private void HandleCameraInput()
        {
            if (!useMobileInput)
            {
                if (Screen.fullScreen || Input.GetMouseButtonDown(0))
                {
                    Cursor.lockState = CursorLockMode.Locked;
                }

                if (Cursor.lockState == CursorLockMode.Locked)
                {
                    float cursorY = Input.GetAxisRaw("Mouse Y");
                    float cursorX = Input.GetAxisRaw("Mouse X");

                    RotateCamera(cursorX, cursorY, mouseSensitvity);
                }

                if (!Screen.fullScreen && Input.GetKeyDown(KeyCode.Escape))
                {
                    Cursor.lockState = CursorLockMode.None;
                }
            }
            else
            {
                if (rightJoystick.IsDragging)
                {
                    Vector2 offset = rightJoystick.GetJoystickOffset();
                    offset  = offset.normalized * Mathf.Pow(offset.magnitude, 2.5f);
                    offset *= Time.deltaTime;
                    RotateCamera(offset.x, offset.y, mobileLookSensitivity);
                }
            }

            bool doJump = Input.GetButtonDown("Jump");

            if (doJump)
            {
                if (isGrounded)
                {
                    Vector3 velocity = playerRigidbody.velocity;
                    velocity.y += jumpVelocity;
                    playerRigidbody.velocity = velocity;
                }
            }
        }