CameraController.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class CameraController : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.mDistance = Vector3.Distance(base.transform.position, this.Target.position);
  10. this.mCurDistance = this.mDistance;
  11. this.mDesiredDistance = this.mDistance;
  12. this.mCurrentRotation = base.transform.rotation;
  13. this.mDesiredRotation = base.transform.rotation;
  14. this.mDegX = base.transform.rotation.eulerAngles.y;
  15. this.mDegY = base.transform.rotation.eulerAngles.x;
  16. }
  17. private float ClampAngle(float angle, float min, float max)
  18. {
  19. if (angle < -360f)
  20. {
  21. angle += 360f;
  22. }
  23. if (angle > 360f)
  24. {
  25. angle -= 360f;
  26. }
  27. return Mathf.Clamp(angle, min, max);
  28. }
  29. private void LateUpdate()
  30. {
  31. base.transform.LookAt(this.Target);
  32. if (Input.GetMouseButton(0))
  33. {
  34. this.mDegX += UnityEngine.Input.GetAxis("Mouse X") * this.RotateSpeed * Time.deltaTime;
  35. this.mDegY -= UnityEngine.Input.GetAxis("Mouse Y") * this.RotateSpeed * Time.deltaTime;
  36. this.mDegY = this.ClampAngle(this.mDegY, (float)this.RotateYMin, (float)this.RotateYMax);
  37. this.mDesiredRotation = Quaternion.Euler(this.mDegY, this.mDegX, 0f);
  38. this.mCurrentRotation = base.transform.rotation;
  39. Quaternion rotation = Quaternion.Lerp(this.mCurrentRotation, this.mDesiredRotation, Time.deltaTime * this.ZoomDampening);
  40. base.transform.rotation = rotation;
  41. }
  42. else if (Input.GetMouseButton(1))
  43. {
  44. this.Target.rotation = base.transform.rotation;
  45. this.Target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * this.panSpeed);
  46. this.Target.Translate(base.transform.up * -Input.GetAxis("Mouse Y") * this.panSpeed, Space.World);
  47. }
  48. this.mDesiredDistance -= UnityEngine.Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * (float)this.ZoomRate * Mathf.Abs(this.mDesiredDistance);
  49. this.mCurDistance = Mathf.Lerp(this.mCurDistance, this.mDesiredDistance, Time.deltaTime * this.ZoomDampening);
  50. Vector3 position = this.Target.position - base.transform.rotation * Vector3.forward * this.mCurDistance;
  51. base.transform.position = position;
  52. }
  53. public Transform Target;
  54. public int ZoomRate = 40;
  55. public float ZoomDampening = 5f;
  56. public float RotateSpeed = 200f;
  57. public int RotateYMin = -80;
  58. public int RotateYMax = 80;
  59. public float panSpeed = 0.3f;
  60. protected float mDistance;
  61. protected float mCurDistance;
  62. protected float mDesiredDistance;
  63. protected Quaternion mCurrentRotation;
  64. protected Quaternion mDesiredRotation;
  65. protected float mDegX;
  66. protected float mDegY;
  67. }
  68. }