SectorDetect.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 扇型攻击检测,并绘制检测区域
  6. /// </summary>
  7. public class SectorDetect : MonoBehaviour
  8. {
  9. public Transform attacked; //受攻击着
  10. public LineRenderer line;
  11. GameObject go;
  12. MeshFilter mf;
  13. MeshRenderer mr;
  14. Shader shader;
  15. void Start()
  16. {
  17. ToDrawSectorSolid(transform, transform.position, 30, 2.5f);
  18. }
  19. void Update()
  20. {
  21. // UmbrellaAttact(transform, attacked.transform, 60, 4);
  22. go.transform.localPosition = transform.localPosition;
  23. }
  24. /// <summary>
  25. /// 扇形攻击范围
  26. /// </summary>
  27. /// <param name="attacker">攻击者</param>
  28. /// <param name="attacked">被攻击方</param>
  29. /// <param name="angle">扇形角度</param>
  30. /// <param name="radius">扇形半径</param>
  31. /// <returns></returns>
  32. public bool UmbrellaAttact(Transform attacker, Transform attacked, float angle, float radius)
  33. {
  34. Vector3 deltaA = attacked.position - attacker.position;
  35. //Mathf.Rad2Deg : 弧度值到度转换常度
  36. //Mathf.Acos(f) : 返回参数f的反余弦值
  37. float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, attacker.forward)) * Mathf.Rad2Deg;
  38. if (tmpAngle < angle * 0.5f && deltaA.magnitude < radius)
  39. {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public void ToDrawSectorSolid(Transform t, Vector3 center, float angle, float radius)
  45. {
  46. int pointAmmount = 100;
  47. float eachAngle = angle / pointAmmount;
  48. Vector3 forward = transform.forward;
  49. List<Vector3> vertices = new List<Vector3>();
  50. Vector3[] linepos = new Vector3[pointAmmount];
  51. vertices.Add(center);
  52. for (int i = 0; i < pointAmmount; i++)
  53. {
  54. Vector3 pos = Quaternion.Euler(0f, -angle / 2 + eachAngle * (i - 1), 0f) * forward * radius + center;
  55. vertices.Add(pos);
  56. linepos[i] = pos;
  57. }
  58. line.positionCount = pointAmmount;
  59. line.SetPositions(linepos);
  60. line.SetPosition(0, transform.position);
  61. line.SetPosition(line.positionCount - 1, transform.position);
  62. CreateMesh(vertices);
  63. }
  64. private GameObject CreateMesh(List<Vector3> vertices)
  65. {
  66. int[] triangles;
  67. Mesh mesh = new Mesh();
  68. int triangleAmount = vertices.Count - 2;
  69. triangles = new int[3 * triangleAmount];
  70. //根据三角形的个数,来计算绘制三角形的顶点顺序
  71. for (int i = 0; i < triangleAmount; i++)
  72. {
  73. triangles[3 * i] = 0;
  74. triangles[3 * i + 1] = i + 1;
  75. triangles[3 * i + 2] = i + 2;
  76. }
  77. if (go == null)
  78. {
  79. go = new GameObject("mesh");
  80. //go.transform.SetParent(transform);
  81. mf = go.AddComponent<MeshFilter>();
  82. mr = go.AddComponent<MeshRenderer>();
  83. shader = Shader.Find("Unlit/Color");
  84. }
  85. mesh.vertices = vertices.ToArray();
  86. mesh.triangles = triangles;
  87. mf.mesh = mesh;
  88. mr.material.shader = shader;
  89. mr.material.color = Color.red;
  90. go.transform.SetParent(transform.parent);
  91. return go;
  92. }
  93. }