Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TPSCamera : MonoBehaviour {
  6.  
  7. public Transform pivot;
  8. public float minRadius = 5;
  9. public float maxRadius = 15;
  10. public float minAzimuth = 0;
  11. public float maxAzimuth = 360;
  12. public float minElevation = 5;
  13. public float maxElevation = 180;
  14. public float resetTime = 5;
  15.  
  16. public float radius = 0;
  17. public float azimuth = 180;
  18. public float elevation = 60;
  19.  
  20. float defoAzimuth, defoElevation;
  21. bool isMove;
  22. float timer;
  23.  
  24. // Use this for initialization
  25. void Start () {
  26. defoAzimuth = azimuth;
  27. defoElevation = elevation;
  28. radius = (minRadius + maxRadius) / 2;
  29. updatePosition();
  30. }
  31.  
  32. // Update is called once per frame
  33. void Update () {
  34. var horizon = -Input.GetAxis("Mouse X");
  35. var vertical = Input.GetAxis("Mouse Y");
  36.  
  37. if (timer > resetTime) {
  38. azimuth = Mathf.Lerp(azimuth, defoAzimuth, Time.deltaTime);
  39. elevation = Mathf.Lerp(elevation, defoElevation, Time.deltaTime);
  40. } else {
  41. azimuth += horizon;
  42. elevation += vertical;
  43. }
  44.  
  45. azimuth = Mathf.Clamp(azimuth, minAzimuth, maxAzimuth);
  46. elevation = Mathf.Clamp(elevation, minElevation, maxElevation);
  47.  
  48. var wheel = Input.GetAxis("Mouse ScrollWheel");
  49. radius += wheel;
  50. radius = Mathf.Clamp(radius, minRadius, maxRadius);
  51.  
  52. if (Mathf.Abs(horizon) > 0 || Mathf.Abs(vertical) > 0 || Mathf.Abs(wheel) > 0) {
  53. isMove = true;
  54. } else {
  55. isMove = false;
  56. }
  57.  
  58. if (!isMove) {
  59. timer += Time.deltaTime;
  60. } else {
  61. timer = 0;
  62. }
  63. }
  64.  
  65. void LateUpdate() {
  66. transform.position = updatePosition();
  67. transform.LookAt(pivot);
  68. }
  69.  
  70. Vector3 updatePosition() {
  71. var offs = 90;
  72. var sinA = Mathf.Sin((azimuth + offs) * Mathf.Deg2Rad);
  73. var cosA = Mathf.Cos((azimuth + offs) * Mathf.Deg2Rad);
  74. var sinE = Mathf.Sin(elevation * Mathf.Deg2Rad);
  75. var cosE = Mathf.Cos(elevation * Mathf.Deg2Rad);
  76. return pivot.position + new Vector3(sinE * cosA, cosE, sinE * sinA) * radius;
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement