public void Update() { if (cameraEnabled) { mouse0 = Input.GetMouseButton(0); mouse1 = Input.GetMouseButton(1); if ((cameraMode == CameraMode.Hold && mouse0 || mouse1) || cameraMode == CameraMode.Always) { x = Input.GetAxis("Mouse X") * mouseSensitivity.x; y = Input.GetAxis("Mouse Y") * mouseSensitivity.y; if (mouseInvertY) { y *= -1.0f; } if (lockMouseCursor) { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } } else { x = 0; y = 0; if (Cursor.lockState == CursorLockMode.Locked) { Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } } if (controllerEnabled && x == 0 && y == 0) { x = Input.GetAxis(rightAxisXName) * controllerSensitivity.x; y = Input.GetAxis(rightAxisYName) * controllerSensitivity.y; if (controllerInvertY) { y *= -1.0f; } } if (Input.GetAxis("Mouse ScrollWheel") < 0) // back { cameraController.desiredDistance += cameraController.zoomOutStepValue; } if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward { cameraController.desiredDistance -= cameraController.zoomOutStepValue; } if (cameraController.desiredDistance < cameraController.minimumDistance) { cameraController.desiredDistance = cameraController.minimumDistance; } Vector3 offsetVectorTransformed = cameraController.Target.transform.rotation * cameraController.offsetVector; transform.RotateAround(cameraController.Target.position + offsetVectorTransformed, Vector3.up, x); yAngle = -y; // Prevent camera flipping angle = Vector3.Angle(transform.forward, upVector); if (yAngle > 0) { if (angle + yAngle > 180.0f) { yAngle = Vector3.Angle(transform.forward, upVector) - 180; if (yAngle < 0) { yAngle = 0; } } } else { if (angle + yAngle < 0.0f) { yAngle = Vector3.Angle(transform.forward, downVector) - 180; } } if (!cameraController.smartPivot || cameraController.cameraNormalMode && (!cameraController.bGroundHit || (cameraController.bGroundHit && y < 0) || transform.position.y > (cameraController.Target.position.y + cameraController.offsetVector.y))) { // normal mode transform.RotateAround(cameraController.Target.position + offsetVectorTransformed, transform.right, yAngle); } else { // smart pivot mode if (smartPivotInit) { smartPivotInit = false; cameraController.InitSmartPivot(); } transform.RotateAround(transform.position, transform.right, yAngle); if (transform.rotation.eulerAngles.x > cameraController.startingY || (transform.rotation.eulerAngles.x >= 0 && transform.rotation.eulerAngles.x < 90)) { smartPivotInit = true; cameraController.DisableSmartPivot(); } } } }
public void LateUpdate() { if (cameraController == null || cameraController.target == null) { return; } if (cameraEnabled) { bool inputFreeLook = cameraMode == CameraMode.Always; // sample mouse inputs if (!inputFreeLook && mouseInput.Count > 0) { for (int i = 0; i < mouseInput.Count && !inputFreeLook; i++) { if (Input.GetMouseButton(mouseInput[i])) { inputFreeLook = true; } } } // sample keyboard inputs if (!inputFreeLook && keyboardInput.Count > 0) { for (int i = 0; i < keyboardInput.Count && !inputFreeLook; i++) { if (Input.GetKey(keyboardInput[i])) { inputFreeLook = true; } } } if (inputFreeLook) { x = Input.GetAxis("Mouse X") * mouseSensitivity.x; y = Input.GetAxis("Mouse Y") * mouseSensitivity.y; if (mouseInvertY) { y *= -1.0f; } if (lockMouseCursor) { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } } else { x = 0; y = 0; if (Cursor.lockState == CursorLockMode.Locked) { Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } } if (controllerEnabled && x == 0 && y == 0) { // sample controller input x = Input.GetAxis(rightAxisXName) * controllerSensitivity.x; y = Input.GetAxis(rightAxisYName) * controllerSensitivity.y; if (controllerInvertY) { y *= -1.0f; } } // sample mouse scrollwheel for zooming in/out if (Input.GetAxis("Mouse ScrollWheel") < 0) // back { cameraController.desiredDistance += cameraController.zoomOutStepValue; if (cameraController.desiredDistance > maxDistance) { cameraController.desiredDistance = maxDistance; } } if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward { cameraController.desiredDistance -= cameraController.zoomOutStepValue; if (cameraController.desiredDistance < minDistance) { cameraController.desiredDistance = minDistance; } } if (cameraController.desiredDistance < 0) { cameraController.desiredDistance = 0; } if (smoothing) { smoothX = Mathf.Lerp(smoothX, x, Time.deltaTime * smoothSpeed); smoothY = Mathf.Lerp(smoothY, y, Time.deltaTime * smoothSpeed); } else { smoothX = x; smoothY = y; } Vector3 offsetVectorTransformed = cameraController.target.transform.rotation * cameraController.offsetVector; transform.RotateAround(cameraController.target.position + offsetVectorTransformed, cameraController.target.up, smoothX); yAngle = -smoothY; // Prevent camera flipping angle = Vector3.Angle(transform.forward, upVector); if (angle <= yAngleLimitMin && yAngle < 0) { yAngle = 0; } if (angle >= yAngleLimitMax && yAngle > 0) { yAngle = 0; } if (yAngle > 0) { if (angle + yAngle > 180.0f) { yAngle = Vector3.Angle(transform.forward, upVector) - 180; if (yAngle < 0) { yAngle = 0; } } } else { if (angle + yAngle < 0.0f) { yAngle = Vector3.Angle(transform.forward, downVector) - 180; } } if (!cameraController.smartPivot || cameraController.cameraNormalMode && (!cameraController.bGroundHit || (cameraController.bGroundHit && y < 0) || transform.position.y > (cameraController.target.position.y + cameraController.offsetVector.y))) { // normal mode transform.RotateAround(cameraController.target.position + offsetVectorTransformed, transform.right, yAngle); } else { // smart pivot mode if (smartPivotInit) { smartPivotInit = false; cameraController.InitSmartPivot(); } transform.RotateAround(transform.position, transform.right, yAngle); if (transform.rotation.eulerAngles.x > cameraController.startingY || (transform.rotation.eulerAngles.x >= 0 && transform.rotation.eulerAngles.x < 90)) { smartPivotInit = true; cameraController.DisableSmartPivot(); } } if (forceCharacterDirection) { cameraController.target.rotation = Quaternion.AngleAxis(transform.rotation.eulerAngles.y, Vector3.up); } } }