当前位置:   article > 正文

Unity3D 范围检测 扇形 圆形 矩形 等腰三角形_unity判断一个图形是否有矩形

unity判断一个图形是否有矩形

代码如下:

  1. using UnityEngine;
  2. public class RangeCheck
  3. {
  4. //--扇形
  5. public static bool CurveRange(Transform self, Transform target, float maxDistance, float maxAngle)
  6. {
  7. return CurveRange(self, target, 0, maxDistance, maxAngle);
  8. }
  9. public static bool CurveRange(Transform self, Transform target, float minDistance, float maxDistance, float maxAngle)
  10. {
  11. Vector3 playerDir = self.forward;
  12. Vector3 enemydir = (target.position - self.position).normalized;
  13. float angle = Vector3.Angle(playerDir, enemydir);
  14. if (angle > maxAngle * 0.5f)
  15. {
  16. return false;
  17. }
  18. float distance = Vector3.Distance(target.position, self.position);
  19. if (distance <= maxDistance && distance >= minDistance)
  20. {
  21. return true;
  22. }
  23. return false;
  24. }
  25. //--圆形
  26. public static bool CircleRange(Transform self, Transform target, float maxDistance)
  27. {
  28. return CircleRange(self, target, 0, maxDistance);
  29. }
  30. public static bool CircleRange(Transform self, Transform target, float minDistance, float maxDistance)
  31. {
  32. float distance = Vector3.Distance(target.position, self.position);
  33. if (distance <= maxDistance && distance >= minDistance)
  34. {
  35. return true;
  36. }
  37. else
  38. {
  39. return false;
  40. }
  41. }
  42. //--矩形
  43. public static bool SquareRange(Transform self, Transform target, float maxWidth, float maxHeight)
  44. {
  45. return SquareRange(self, target, maxWidth, 0, maxHeight);
  46. }
  47. public static bool SquareRange(Transform self, Transform target, float maxWidth, float minHeight, float maxHeight)
  48. {
  49. Vector3 enemyDir = (target.position - self.position).normalized;
  50. float angle = Vector3.Angle(enemyDir, self.forward);
  51. if (angle > 90)
  52. {
  53. return false;
  54. }
  55. float distance = Vector3.Distance(target.position, self.position);
  56. float z = distance * Mathf.Cos(angle * Mathf.Deg2Rad);
  57. float x = distance * Mathf.Sin(angle * Mathf.Deg2Rad);
  58. if (x <= maxWidth * 0.5f && z <= maxHeight && z >= minHeight)
  59. {
  60. return true;
  61. }
  62. return false;
  63. }
  64. //--等腰三角形
  65. public static bool TriangleRange(Transform self, Transform target, float maxDistance, float maxAngle)
  66. {
  67. Vector3 playerDir = self.forward;
  68. Vector3 enemydir = (target.position - self.position).normalized;
  69. float angle = Vector3.Angle(playerDir, enemydir);
  70. if (angle > maxAngle * 0.5f)
  71. {
  72. return false;
  73. }
  74. float angleDistance = maxDistance * Mathf.Cos(maxAngle * 0.5f * Mathf.Deg2Rad) / Mathf.Cos(angle * Mathf.Deg2Rad);
  75. float distance = Vector3.Distance(target.position, self.position);
  76. if (distance <= angleDistance)
  77. {
  78. return true;
  79. }
  80. return false;
  81. }
  82. }

应用代码如下:

  1. using UnityEngine;
  2. public class RangeTest : MonoBehaviour
  3. {
  4. private GameObject cube1;
  5. private GameObject cube2;
  6. private void Start()
  7. {
  8. cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  9. cube1.transform.position = Vector3.zero;
  10. cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  11. cube2.transform.position = new Vector3(0, 0, 4);
  12. Debug.Log(cube1.transform.position.ToString());
  13. Debug.Log(cube2.transform.position.ToString());
  14. bool isInCurveRange = RangeCheck.CurveRange(cube1.transform, cube2.transform, 3.9f, 90);
  15. Debug.Log(isInCurveRange);
  16. bool isInCircleRange = RangeCheck.CircleRange(cube1.transform, cube2.transform, 4.1f, 5);
  17. Debug.Log(isInCircleRange);
  18. bool isInSquareRange = RangeCheck.SquareRange(cube1.transform, cube2.transform, 5, 4.1f, 10);
  19. Debug.Log(isInSquareRange);
  20. bool isInTriangleRange = RangeCheck.TriangleRange(cube1.transform, cube2.transform, 5, 60);
  21. Debug.Log(isInTriangleRange);
  22. }
  23. }

参考了:http://www.manew.com/blog-228538-44293.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/108051
推荐阅读
相关标签
  

闽ICP备14008679号