Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using System.Collections;
- namespace HD
- {
- public class CameraController
- {
- #region Data
- const float moveSpeed = 0.3f;
- const float scroolSpeed = 0.06f;
- static readonly Vector3 closeWP = new Vector3(0, 2, 0);
- static readonly Vector3 farWP = new Vector3(0, 30, 0);
- readonly UnityEngine.Transform cameraHolderTransform;
- readonly UnityEngine.Transform cameraTransform;
- Vector3? lastMousePosition;
- Vector3 targetCameraPosition;
- float targetScale = .5f;
- float currentScale = .5f;
- Quaternion targetHolderRotation, lasttargetHolderRotation, lastHook = Quaternion.identity;
- #endregion
- #region Properties
- float deltaTime
- {
- get
- {
- return Time.unscaledDeltaTime;
- }
- }
- #endregion
- #region Init
- public CameraController()
- {
- cameraHolderTransform = GameObject.Find("CameraHolder")?.transform;
- if(cameraHolderTransform == null)
- { // This is a unit test run
- return;
- }
- cameraTransform = Camera.main.transform;
- targetCameraPosition = cameraHolderTransform.position;
- lasttargetHolderRotation = targetHolderRotation = cameraHolderTransform.rotation;
- lastMousePosition = Input.mousePosition;
- UpdateCameraTransform();
- targetCameraPosition = cameraHolderTransform.position = Vector3.zero;
- }
- #endregion
- #region Events
- public void OnUpdate()
- {
- if(cameraHolderTransform == null)
- {
- return;
- }
- if(MoveCamera() | RotateCamera() | ZoomCamera())
- {
- }
- UpdateCameraTransform();
- Culler.instance.OnCameraChange();
- lastMousePosition = Input.mousePosition;
- }
- #endregion
- #region Private Write API
- void UpdateCameraTransform()
- {
- var lastScale = currentScale;
- currentScale = Mathf.Lerp(currentScale, targetScale, 3.5f * deltaTime);
- Controllers.mouseController.RefreshMousePosition();
- Vector3? originalMousePosition = Controllers.mouseController.mousePosition;
- cameraTransform.localPosition = Vector3.Lerp(closeWP, farWP, currentScale);
- cameraTransform.localRotation = Quaternion.Lerp(cameraTransform.localRotation,
- targetHolderRotation * lastHook,
- (HDMath.Range(1 - currentScale, .1f, .8f)) * 20 * deltaTime);
- if(currentScale > .2f)
- { // Attempt to hover over the same location
- Controllers.mouseController.RefreshMousePosition();
- Vector3? newMousePosition = Controllers.mouseController.mousePosition;
- if(newMousePosition != null && originalMousePosition != null)
- {
- Vector3 deltaMousePosition = originalMousePosition.Value - newMousePosition.Value;
- deltaMousePosition *= 1.3f;
- targetCameraPosition += deltaMousePosition;
- cameraHolderTransform.position += deltaMousePosition;
- }
- }
- lastHook = Quaternion.Euler(
- new Vector3(35 + 55 * Math.Min(1, 3f * currentScale), 0, 0));
- cameraTransform.localRotation = Quaternion.Lerp(cameraTransform.localRotation,
- targetHolderRotation * lastHook,
- (HDMath.Range(1 - currentScale, .1f, .8f)) * 20 * deltaTime);
- cameraHolderTransform.localPosition
- = Vector3.Lerp(cameraHolderTransform.localPosition, targetCameraPosition, 7.5f * deltaTime);
- lasttargetHolderRotation = targetHolderRotation;
- }
- bool MoveCamera()
- {
- Vector3 moveVector = Vector3.zero;
- if(Input.IsDown(InputType.CameraForward))
- {
- moveVector += Vector3.forward;
- }
- if(Input.IsDown(InputType.CameraLeft))
- {
- moveVector += Vector3.left;
- }
- if(Input.IsDown(InputType.CameraBackward))
- {
- moveVector += Vector3.back;
- }
- if(Input.IsDown(InputType.CameraRight))
- {
- moveVector += Vector3.right;
- }
- if(moveVector == Vector3.zero)
- {
- return false;
- }
- targetCameraPosition += (targetHolderRotation * moveVector) * (currentScale * .85f + .15f) * 50f * deltaTime;
- return true;
- }
- bool RotateCamera()
- {
- if(Input.mousePosition == null || lastMousePosition == null)
- {
- return false;
- }
- if(Input.IsDown(InputType.CameraRotate) == false)
- {
- return false;
- }
- float deltaX = lastMousePosition.Value.x - Input.mousePosition.Value.x;
- float deltaY = Input.mousePosition.Value.y - lastMousePosition.Value.y;
- targetHolderRotation *= Quaternion.Euler(new Vector3(0, (deltaX + deltaY) * .5f * HDMath.Range(currentScale, .4f, .8f), 0));
- return true;
- }
- bool ZoomCamera()
- {
- if(EventSystem.current.IsPointerOverGameObject())
- {
- // we are hovering over a UI option
- return false;
- }
- targetScale += UnityEngine.Input.mouseScrollDelta.y * scroolSpeed;
- if(targetScale == 0)
- {
- return false;
- }
- if(targetScale > 1)
- {
- targetScale = 1;
- }
- if(targetScale < 0)
- {
- targetScale = 0;
- }
- return true;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement