Guest User

Untitled

a guest
Nov 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RCameraControls : MonoBehaviour {
  6.  
  7. public Transform target;
  8. public Vector3 offset;
  9. public bool useOffsetValues;
  10. public float rotateSpeed;
  11. public Transform pivot;
  12. public float maxViewAngle;
  13. public float minViewAngle;
  14. public bool InvertY;
  15.  
  16.  
  17. // Use this for initialization
  18. void Start () {
  19. if (!useOffsetValues)
  20. {
  21. offset = target.position - transform.position;
  22. }
  23.  
  24. pivot.transform.position = target.transform.position;
  25. pivot.transform.parent = target.transform;
  26.  
  27. Cursor.lockState = CursorLockMode.Locked;
  28. }
  29.  
  30.  
  31. // Update is called once per frame
  32. void LateUpdate () {
  33. //Поворот камеры по иксу
  34. float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
  35. target.Rotate(0, horizontal, 0);
  36. // поворот по игрику
  37. float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
  38. pivot.Rotate(-vertical, 0, 0);
  39. if (InvertY)
  40. {
  41. pivot.Rotate(vertical, 0, 0);
  42. }
  43. else
  44. {
  45. pivot.Rotate(-vertical, 0, 0);
  46. }
  47.  
  48. //Верхний и нижний угол камеры
  49. if(pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
  50. {
  51. pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
  52. }
  53.  
  54. if(pivot.rotation.eulerAngles.x > 180 && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
  55. {
  56. pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
  57. }
  58.  
  59. //движение камеры в зависимости от позиции
  60. float desiredYAngle = target.eulerAngles.y;
  61. float desiredXAngle = pivot.eulerAngles.x;
  62. Quaternion rotation = Quaternion.Euler(desiredXAngle , desiredYAngle, 0);
  63. transform.position = target.position - ( rotation * offset);
  64.  
  65. //изминение позиции
  66. if(transform.position.y < target.position.y)
  67. {
  68. transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
  69. }
  70.  
  71. transform.LookAt(target);
  72. }
  73. }
Add Comment
Please, Sign In to add comment