MathfX.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public static class MathfX
  5. {
  6. public static Vector3 RotateVector(Vector3 v, float angle)
  7. {
  8. return Quaternion.AngleAxis(angle, Vector3.back) * v;
  9. }
  10. public static Vector2 Intersect2DCenter(Bounds bounds1, Bounds bounds2)
  11. {
  12. float magnitude = bounds1.size.magnitude;
  13. float magnitude2 = bounds2.size.magnitude;
  14. Vector2 a = bounds2.center - bounds1.center;
  15. Vector2 a2 = a * magnitude / (magnitude + magnitude2);
  16. return a2 + (Vector2)bounds1.center;
  17. }
  18. public static Vector2[] Intersect2DCenterMCM(Bounds player, Collider2D other)
  19. {
  20. float num = 0.1f;
  21. Bounds bounds = player;
  22. List<Vector2> list = new List<Vector2>();
  23. float x = bounds.min.x;
  24. float x2 = bounds.max.x;
  25. float y = bounds.min.y;
  26. float y2 = bounds.max.y;
  27. for (float num2 = x; num2 < x2; num2 += num)
  28. {
  29. for (float num3 = y; num3 < y2; num3 += num)
  30. {
  31. Vector2 vector = new Vector2(num2, num3);
  32. if (other.OverlapPoint(vector))
  33. {
  34. list.Add(vector);
  35. }
  36. }
  37. }
  38. if (list.Count != 0)
  39. {
  40. return list.ToArray();
  41. }
  42. return new Vector2[]
  43. {
  44. MathfX.Intersect2DCenter(player, other.bounds)
  45. };
  46. }
  47. public static bool isInRange(float x, float rangeMin, float rangeMax)
  48. {
  49. return x > rangeMin && x < rangeMax;
  50. }
  51. public static bool isInMiddleRange(float x, float middle, float diameter)
  52. {
  53. diameter = Mathf.Abs(diameter);
  54. return MathfX.isInRange(x, middle - diameter / 2f, middle + diameter / 2f);
  55. }
  56. public static GameObject NearestObjOnXAxis(GameObject aim, ICollection<GameObject> objects)
  57. {
  58. float num = float.MaxValue;
  59. GameObject result = null;
  60. foreach (GameObject gameObject in objects)
  61. {
  62. float num2 = Mathf.Abs(aim.transform.position.x - gameObject.transform.position.x);
  63. if (num2 < num)
  64. {
  65. num = num2;
  66. result = gameObject;
  67. }
  68. }
  69. return result;
  70. }
  71. public static float PosToHeight(Vector3 pos)
  72. {
  73. return pos.y * 1.957f;
  74. }
  75. public static float HeightToPos(float height)
  76. {
  77. return 0.511f * height;
  78. }
  79. }