123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class MainCameraLogic : MonoBehaviour
- {
-
- private GameObject mainCamera;
-
- public Button btnResetView;
- private bool wPressed;
- private bool sPressed;
- private bool aPressed;
- private bool dPressed;
-
- public Transform target;
- private int MouseWheelSensitivity = 3;
- private int MouseZoomMin = -50;
- private int MouseZoomMax = 50;
-
- const float centerMoveSpeed = 0.01f;
- private const int keyMoveSpeed = 1;
- private float moveSpeed = 5;
- private float xSpeed = 80.0f;
- private float ySpeed = 80.0f;
- private int yMinLimit = -360;
- private int yMaxLimit = 360;
- private float x = 0.0f;
- private float y = 0.0f;
- private float Distance = 5;
- private Vector3 targetOnScreenPosition;
- private Quaternion storeRotation;
- private Vector3 CameraTargetPosition;
- private Vector3 initPosition;
- private Vector3 cameraX;
- private Vector3 cameraY;
- private Vector3 cameraZ;
- private Vector3 initScreenPos;
- private Vector3 curScreenPos;
-
- void Start()
- {
- mainCamera = GameObject.Find("Main Camera");
-
-
-
-
-
-
- }
-
- void Update()
- {
-
- var angles = transform.eulerAngles;
- x = angles.y;
- y = angles.x;
- CameraTargetPosition = target.position;
- target.rotation = transform.rotation;
-
-
- storeRotation = Quaternion.Euler(y, x, 0);
- transform.rotation = storeRotation;
- Vector3 position = storeRotation * new Vector3(0.0F, 0.0F, -Distance) + CameraTargetPosition;
- transform.position = storeRotation * new Vector3(0, 0, -Distance) + CameraTargetPosition;
-
- if (Input.GetMouseButton(1))
- {
- x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
- y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
- y = ClampAngle(y, yMinLimit, yMaxLimit);
-
- storeRotation = Quaternion.Euler(y, x, 0);
- var tmpPosition = storeRotation * new Vector3(0.0f, 0.0f, -Distance) + CameraTargetPosition;
- transform.rotation = storeRotation;
- transform.position = tmpPosition;
- }
- else if (Input.GetAxis("Mouse ScrollWheel") != 0)
- {
- if (Distance >= MouseZoomMin && Distance <= MouseZoomMax)
- {
- Distance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
- }
- if (Distance < MouseZoomMin)
- {
- Distance = MouseZoomMin;
- }
- if (Distance > MouseZoomMax)
- {
- Distance = MouseZoomMax;
- }
- var rotation = transform.rotation;
- transform.position = storeRotation * new Vector3(0.0F, 0.0F, -Distance) + CameraTargetPosition;
- }
-
- if (Input.GetMouseButtonDown(2))
- {
- cameraX = transform.right;
- cameraY = transform.up;
- cameraZ = transform.forward;
- initScreenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetOnScreenPosition.z);
- Debug.Log("downOnce");
-
- targetOnScreenPosition = Camera.main.WorldToScreenPoint(CameraTargetPosition);
- initPosition = CameraTargetPosition;
- }
- if (Input.GetMouseButton(2))
- {
- curScreenPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetOnScreenPosition.z);
-
-
- target.position = initPosition - centerMoveSpeed * ((curScreenPos.x - initScreenPos.x) * cameraX + (curScreenPos.y - initScreenPos.y) * cameraY);
-
- Vector3 mPosition = storeRotation * new Vector3(0.0F, 0.0F, -Distance) + target.position;
- transform.position = mPosition;
-
-
- }
- if (Input.GetMouseButtonUp(2))
- {
- Debug.Log("upOnce");
-
- CameraTargetPosition = target.position;
- }
- if (Input.GetKeyDown(KeyCode.D))
- {
- dPressed = true;
- }
- if (Input.GetKeyDown(KeyCode.A))
- {
- aPressed = true;
- }
- if (Input.GetKeyUp(KeyCode.A))
- {
- Debug.Log("A up");
- aPressed = false;
- }
- if (Input.GetKeyUp(KeyCode.W))
- {
- Debug.Log("W up");
- wPressed = false;
- }
- if (Input.GetKeyUp(KeyCode.S))
- {
- Debug.Log("s up");
- sPressed = false;
- }
- if (Input.GetKeyUp(KeyCode.D))
- {
- Debug.Log("d up");
- dPressed = false;
- }
- if (Input.GetKeyDown(KeyCode.W))
- {
- wPressed = true;
- Debug.Log("W Pressed");
- }
- if (Input.GetKeyDown(KeyCode.S))
- {
- sPressed = true;
- }
- keyMove();
- }
- void keyMove()
- {
- if (wPressed)
- {
-
- target.transform.Translate(0, 0, keyMoveSpeed * Time.deltaTime, Space.Self);
- }
- if (sPressed)
- {
-
- target.transform.Translate(0, 0, -keyMoveSpeed * Time.deltaTime, Space.Self);
- }
- if (dPressed)
- {
-
-
-
-
-
-
-
-
- target.transform.Translate(keyMoveSpeed * Time.deltaTime, 0, 0, Space.Self);
- }
- if (aPressed)
- {
-
-
-
-
-
-
-
-
- target.transform.Translate(-keyMoveSpeed * Time.deltaTime,0,0, Space.Self);
- }
- }
-
- static float ClampAngle(float angle, float min, float max)
- {
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp(angle, min, max);
- }
- void testScreenToWorldPoint()
- {
-
- Vector3 screenPoint = Camera.main.WorldToScreenPoint(CameraTargetPosition);
- Debug.Log("ScreenPoint: " + screenPoint);
-
-
- }
-
- public void ResetView()
- {
- Debug.Log("Reset View!!!");
- Vector3 position = new Vector3(62f, 78f, -322f);
- mainCamera.transform.position = position;
- target.transform.position = position;
- Quaternion rotation = Quaternion.Euler(32, 183, 0);
- mainCamera.transform.rotation = rotation;
-
- Distance = 5;
- }
- }
|