Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerCamera : MonoBehaviour
  6. {
  7. [System.Serializable]
  8. public class InputSettings : System.Object
  9. {
  10. public float inputThreshold = 0.1f;
  11. public string VERTICAL_AXIS = "CameraVertical";
  12. public string HORIZONTAL_AXIS = "CameraHorizontal";
  13. public string LOCK_TARGET_PYRAMID = "LockToPyramid";
  14. public string LOCK_TARGET_AREA = "LockToArea";
  15. }
  16.  
  17. [SerializeField] private Transform target;
  18. [SerializeField] private float cameraSpeed = 2;
  19. [SerializeField] private Vector3 lookAtPlayerOffset = new Vector3(0f, 0.6f);
  20.  
  21. private PlayerController playerController;
  22. private float cameraHorizontal, cameraVertical;
  23. private float lockPyramid, lockArea;
  24. private InputSettings inputSettings;
  25. private Vector3 cameraPlayerOffset;
  26.  
  27. void Start()
  28. {
  29. SetCameraTarget(target);
  30.  
  31. inputSettings = new InputSettings();
  32. cameraPlayerOffset = transform.position - playerController.transform.position;
  33. }
  34.  
  35. private void Update()
  36. {
  37. GetInput();
  38. }
  39.  
  40. private void LateUpdate()
  41. {
  42. RotateCamera();
  43. UpdateCameraPosition();
  44. }
  45.  
  46. private void RotateCamera()
  47. {
  48. Quaternion cameraRotationXDelta = Quaternion.AngleAxis(cameraHorizontal * cameraSpeed, Vector3.up);
  49. Quaternion cameraRotationYDelta = Quaternion.AngleAxis(cameraVertical * cameraSpeed, Vector3.right);
  50.  
  51. // this does not work how I wish it would after rotating around a bit
  52. cameraPlayerOffset = cameraRotationYDelta * cameraRotationXDelta * cameraPlayerOffset;
  53.  
  54. }
  55.  
  56. private void UpdateCameraPosition()
  57. {
  58. // prevent camera from clipping through floor
  59. Vector3 newPos = playerController.transform.position + cameraPlayerOffset;
  60. newPos.y = newPos.y < 0.2f ? newPos.y = 0.2f : newPos.y;
  61. transform.position = newPos;
  62. transform.LookAt(target.transform.position + lookAtPlayerOffset);
  63. }
  64.  
  65. private void GetInput()
  66. {
  67. cameraHorizontal = Input.GetAxis(inputSettings.HORIZONTAL_AXIS); // interpolated
  68. cameraVertical = Input.GetAxis(inputSettings.VERTICAL_AXIS); // interpolated
  69.  
  70. lockPyramid = Input.GetAxis(inputSettings.LOCK_TARGET_PYRAMID); // interpolated
  71. lockArea = Input.GetAxis(inputSettings.LOCK_TARGET_AREA); // interpolated
  72. }
  73.  
  74. private void SetCameraTarget(Transform targetTransform)
  75. {
  76. target = targetTransform;
  77.  
  78. if (target != null)
  79. {
  80. if (target.GetComponent<PlayerController>())
  81. {
  82. playerController = target.GetComponent<PlayerController>();
  83. }
  84. else
  85. {
  86. Debug.LogError("The camera's target needs a player character controller.");
  87. }
  88. }
  89. else
  90. {
  91. Debug.LogError("Your camera needs a target");
  92. }
  93. }
  94. }
  95.  
  96. Quaternion cameraRotationYDelta = Quaternion.AngleAxis(cameraVertical * cameraSpeed, Vector3.right);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement