Пример #1
0
 void Start()
 {
     neck           = GetComponent <Player>().neck;
     pusher         = Camera.main.GetComponent <CameraPusher>();
     playerSettings = GetComponent <Player>().playerPrefs.playerSettings;
     smoothing      = playerSettings.smoothing;
 }
Пример #2
0
    void Update()
    {
        if (CameraOBJ == null && Camera.main != null)
        {
            CameraOBJ = Camera.main.gameObject;
        }
        if (pusher == null && CameraOBJ != null)
        {
            pusher = CameraOBJ.GetComponent <CameraPusher>();
        }

        sensitivity = new Vector2(playerSettings.xSensitivity, playerSettings.ySensitivity);
        hurtOffset  = Vector3.Lerp(hurtOffset, Vector3.zero, Time.deltaTime * 3);

        if (!freeze)
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
        else
        {
            Cursor.lockState = CursorLockMode.None;
        }

        if (freeze)
        {
            return;
        }

        // Allow the script to clamp based on a desired target value.
        var targetOrientation = Quaternion.Euler(rotationOffset);

        // Get raw mouse input for a cleaner reading on more sensitive mice.
        var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        // Scale input against the sensitivity setting and multiply that against the smoothing value.
        mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing, sensitivity.y * smoothing));

        // Interpolate mouse movement over time to apply smoothing delta.
        _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing);
        _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing);

        // Find the absolute mouse movement value from point zero.
        _mouseAbsolute += _smoothMouse;

        // Clamp and apply the local x value first, so as not to be affected by world transforms.
        if (viewRange.x < 360)
        {
            _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -viewRange.x * 0.5f + viewRange.x, viewRange.x * 0.5f + viewRange.x);
        }

        // Then clamp and apply the global y value.
        if (viewRange.y < 360)
        {
            _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -viewRange.y * 0.5f + viewRange.y, viewRange.y * 0.5f + viewRange.y);
        }

        transform.localRotation = Quaternion.AngleAxis(0, targetOrientation * Vector3.right) * targetOrientation;

        var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));

        transform.localRotation *= yRotation * Quaternion.Euler(0, hurtOffset.x, hurtOffset.y);
    }