Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Mirror;
  6.  
  7. namespace DM
  8. {
  9. public class CameraManager : NetworkBehaviour
  10. {
  11. public float followSpeed = .0001f; //defines camera speed that follows character.
  12. public float mouseSpeed = .0001f; //defines mouse's control speed.
  13. public float controllerSpeed = .0001f; //defines joypad's control speed.
  14.  
  15. public Transform target; //stores the target for camera.
  16.  
  17. [HideInInspector]
  18. public Transform pivot; //pivot for camera's rotation.
  19. [HideInInspector]
  20. public Transform camTrans; //stores camera's root transform.
  21.  
  22. float turnSmoothing = 1f; //defines how smooth this camera moves.
  23. public float minAngle = -45; //minimum vertical angle.
  24. public float maxAngle = 45f; //maximum vertical angle.
  25.  
  26. public float minLook = -45; //minimum vertical angle.
  27. public float maxLook = 45f; //maximum vertical angle.
  28.  
  29. float smoothX;
  30. float smoothY;
  31. float smoothXVelocity;
  32. float smoothYVelocity;
  33. public float lookAngle;
  34. public float tiltAngle;
  35.  
  36. float h;
  37. float v;
  38.  
  39. public void Update()
  40. {
  41. target = this.transform.parent;
  42. }
  43.  
  44. public void Init(Transform t) //Initiallize camera settings.
  45. {
  46. target = t;
  47. camTrans = Camera.main.transform;
  48. pivot = camTrans.parent;
  49. }
  50.  
  51. public void FixedTick(float d) //Getting camera inputs and updates camera's stats.
  52. {
  53.  
  54. if (isLocalPlayer) {
  55. h = Input.GetAxis("Mouse X");
  56. v = Input.GetAxis("Mouse Y");
  57. }
  58.  
  59. //float c_h = Input.GetAxis("RightAxis X");
  60. //float c_v = Input.GetAxis("RightAxis Y");
  61.  
  62. float targetSpeed = mouseSpeed;
  63.  
  64.  
  65. //if(c_h != 0 || c_v != 0)
  66. //{
  67. //h = c_h;
  68. //v = c_v;
  69. //targetSpeed = controllerSpeed;
  70. //}
  71.  
  72. FollowTarget(d);
  73. HandleRotations(d, v, h, targetSpeed);
  74. }
  75.  
  76. void FollowTarget(float d) //defines how camera follows the target.
  77. {
  78. float speed = d * followSpeed;
  79. Vector3 targetPosition = Vector3.Lerp(transform.position, target.position, speed);
  80. targetPosition.y += 0.5F;
  81. targetPosition.z += 0.5F;
  82.  
  83. transform.position = targetPosition;
  84. }
  85.  
  86. void HandleRotations(float d, float v, float h, float targetSpeed) //defines the rotation of camera.
  87. {
  88. if(turnSmoothing > 0)
  89. {
  90. smoothX = Mathf.SmoothDamp(smoothX, h, ref smoothXVelocity, turnSmoothing);
  91. smoothY = Mathf.SmoothDamp(smoothY, v, ref smoothYVelocity, turnSmoothing);
  92. }
  93. else
  94. {
  95. smoothX = h;
  96. smoothY = v;
  97. }
  98.  
  99. lookAngle += smoothX * targetSpeed;
  100. // lookAngle = Mathf.Clamp(lookAngle, minLook, maxLook);
  101. transform.rotation = Quaternion.Euler(0, lookAngle, 0);
  102.  
  103. tiltAngle -= smoothY * targetSpeed;
  104. tiltAngle = Mathf.Clamp(tiltAngle, minAngle, maxAngle);
  105. pivot.localRotation = Quaternion.Euler(tiltAngle, 0, 0);
  106.  
  107. }
  108.  
  109. public static CameraManager singleton;
  110.  
  111. private void Awake()
  112. {
  113. singleton = this;
  114. }
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement