CameraScript.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraScript : MonoBehaviour
  5. {
  6. //enum CameraState {Idle, ZoomingIn, ZoomedIn, ZoomingOut };
  7. //CameraState currentState;
  8. bool isZooming;
  9. float time;
  10. bool isZoomed;
  11. float defaultSize;
  12. float targetSize;
  13. float startSize;
  14. Vector3 DefaultPosition;
  15. Vector3 StartPosition;
  16. Vector3 targetPosition;
  17. Camera myCamera;
  18. // Use this for initialization
  19. void Awake()
  20. {
  21. myCamera = GetComponent<Camera>();
  22. defaultSize = myCamera.orthographicSize;
  23. //DefaultPosition = transform.position;
  24. }
  25. public void GoTo(float zoom, Vector3 CharPos)
  26. {
  27. isZooming = true;
  28. time = 0;
  29. //CameraLocation camLocation = CameraLocations.instance.GetLocation();
  30. startSize = myCamera.orthographicSize;
  31. targetSize = zoom;
  32. StartPosition = transform.position;
  33. targetPosition = CharPos;
  34. if (!isZoomed)
  35. {
  36. defaultSize = startSize;
  37. DefaultPosition = StartPosition;
  38. }
  39. isZoomed = true;
  40. }
  41. public void ZoomOut()
  42. {
  43. isZooming = true;
  44. time = 0;
  45. startSize = targetSize;
  46. targetSize = defaultSize;
  47. //DefaultPosition = StartPosition;
  48. StartPosition = targetPosition;
  49. targetPosition = DefaultPosition;
  50. StartCoroutine(ReachPosition());
  51. }
  52. IEnumerator ReachPosition()
  53. {
  54. while(isZooming)
  55. {
  56. yield return new WaitForSeconds(0.1f);
  57. }
  58. isZoomed = false;
  59. }
  60. // Update is called once per frame
  61. void Update()
  62. {
  63. if (isZooming)
  64. {
  65. time += Time.deltaTime * 2;
  66. myCamera.orthographicSize = Mathf.Lerp(startSize, targetSize, time);
  67. transform.position = Vector3.Lerp(StartPosition, targetPosition, time);
  68. if (time >= 1)
  69. {
  70. myCamera.orthographicSize = targetSize;
  71. transform.position = targetPosition;
  72. isZooming = false;
  73. }
  74. }
  75. }
  76. }