Advertisement
shadowx320

CameraManager.cs

Apr 9th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace SA
  6. {
  7. public class CameraManager : MonoBehaviour
  8. {
  9. public bool lockon;
  10. public float followSpeed = 9;
  11. public float mouseSpeed = 2;
  12. public float controllerSpeed = 7;
  13.  
  14. public Transform target;
  15. public EnemyTarget lockonTarget;
  16. public Transform lockonTransform;
  17.  
  18. [HideInInspector]
  19. public Transform pivot;
  20. [HideInInspector]
  21. public Transform camTrans;
  22. StateManager states;
  23.  
  24. float turnSmoothing = .1f;
  25. public float minAngle = -35f;
  26. public float maxAngle = 35f;
  27.  
  28. float smoothX;
  29. float smoothY;
  30. float smoothXvelocity;
  31. float smoothYvelocity;
  32. public float lookAngle;
  33. public float tiltAngle;
  34.  
  35. bool usedRightAxis;
  36.  
  37. public void Init(StateManager st)
  38. {
  39. states = st;
  40. target = st.transform;
  41.  
  42. camTrans = Camera.main.transform;
  43. pivot = camTrans.parent;
  44. }
  45.  
  46. public void Tick(float d)
  47. {
  48. float h = Input.GetAxis("Mouse X");
  49. float v = Input.GetAxis("Mouse Y");
  50.  
  51. float c_h = Input.GetAxis("RightAxis X");
  52. float c_v = Input.GetAxis("RightAxis Y");
  53.  
  54. float targetSpeed = mouseSpeed;
  55.  
  56. if (lockonTarget != null)
  57. {
  58. if(lockonTransform == null)
  59. {
  60. lockonTransform = lockonTarget.GetTarget();
  61. states.lockOnTransform = lockonTransform;
  62. }
  63.  
  64. if(Mathf.Abs(c_h) > 0.6f)
  65. {
  66. if(!usedRightAxis)
  67. {
  68. lockonTransform = lockonTarget.GetTarget((c_h > 0));
  69. states.lockOnTransform = lockonTransform;
  70. usedRightAxis = true;
  71. }
  72. }
  73. }
  74.  
  75. if(usedRightAxis)
  76. {
  77. if(Mathf.Abs(c_h) < 0.6f)
  78. {
  79. usedRightAxis = false;
  80. }
  81. }
  82.  
  83. if(c_h != 0 || c_v != 0)
  84. {
  85. h = c_h;
  86. v= c_v;
  87. targetSpeed = controllerSpeed;
  88. }
  89.  
  90. FollowTarget(d);
  91. HandleRotations(d, v, h, targetSpeed);
  92. }
  93.  
  94. void FollowTarget(float d)
  95. {
  96. float speed = d * followSpeed;
  97. Vector3 targetPosition = Vector3.Lerp(transform.position, target.position, speed);
  98. transform.position = targetPosition;
  99. }
  100.  
  101. void HandleRotations(float d, float v, float h, float targetSpeed)
  102. {
  103. if(turnSmoothing > 0)
  104. {
  105. smoothX = Mathf.SmoothDamp(smoothX, h, ref smoothXvelocity, turnSmoothing);
  106. smoothY = Mathf.SmoothDamp(smoothY, v, ref smoothYvelocity, turnSmoothing);
  107. }
  108. else
  109. {
  110. smoothX = h;
  111. smoothY = v;
  112. }
  113.  
  114. tiltAngle -= smoothY * targetSpeed;
  115. tiltAngle = Mathf.Clamp(tiltAngle, minAngle, maxAngle);
  116. pivot.localRotation = Quaternion.Euler(tiltAngle, 0, 0);
  117.  
  118. if(lockon && lockonTarget != null)
  119. {
  120. Vector3 targetDir = lockonTransform.position - transform.position;
  121. targetDir.Normalize();
  122. //targetDir.y = 0;
  123.  
  124. if(targetDir == Vector3.zero)
  125. targetDir = transform.forward;
  126. Quaternion targetRot = Quaternion.LookRotation(targetDir);
  127. transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, d * 9);
  128. lookAngle = transform.eulerAngles.y;
  129.  
  130. return;
  131. }
  132.  
  133. lookAngle += smoothX * targetSpeed;
  134. transform.rotation = Quaternion.Euler(0, lookAngle, 0);
  135. }
  136.  
  137. public static CameraManager singleton;
  138. void Awake()
  139. {
  140. singleton = this;
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement