CrossHair.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. namespace DuckHunter
  6. {
  7. public class CrossHair : MonoBehaviour
  8. {
  9. public static CrossHair Instance;
  10. void Start()
  11. {
  12. Instance = this;
  13. InitRotateRangeVH();
  14. }
  15. void OnDestroy()
  16. {
  17. if (Instance == this) Instance = null;
  18. }
  19. Vector2 mapPosition;
  20. float rotateRangeV;
  21. float rotateRangeH;
  22. void InitRotateRangeVH()
  23. {
  24. rotateRangeV = 25f;
  25. float halfScreenHeight = Screen.height * 0.5f;
  26. float halfScreenWidth = Screen.width * 0.5f;
  27. float halfRangeV = rotateRangeV * 0.5f;
  28. float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
  29. float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
  30. rotateRangeH = halfRangeH * 2f;
  31. }
  32. public void UpdatePositionByModuleRotation(Quaternion rotation)
  33. {
  34. Vector3 resEulerAngles = rotation.eulerAngles;
  35. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
  36. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
  37. mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
  38. mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
  39. transform.position = mapPosition;
  40. }
  41. public void Shoot()
  42. {
  43. AudioManager.Instance.PlayShoot(null);
  44. _PlayShootAnimation();
  45. }
  46. Sequence _shootSequence;
  47. void _PlayShootAnimation()
  48. {
  49. if (_shootSequence != null)
  50. {
  51. _shootSequence.Kill();
  52. _shootSequence = null;
  53. }
  54. _shootSequence = DOTween.Sequence();
  55. _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
  56. _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
  57. }
  58. }
  59. }