Unity 攻击范围的检测
一、扇形攻击范围检测
1 using System.Collections.Generic; 2 using UnityEngine; 3 4 public class AttackRange : MonoBehaviour 5 { 6 // 攻击目标 7 public Transform Target; 8 // 扇形范围大小 9 private float SkillDistance = 10; 10 // 扇形的角度 11 private float SkillJiaodu = 90; 12 13 private void Update() 14 { 15 // 与敌人的距离 16 float distance = Vector3.Distance(transform.position, Target.position); 17 // 玩家正前方的向量 18 Vector3 norVec = transform.rotation * Vector3.forward; 19 // 玩家与敌人的方向向量 20 Vector3 temVec = Target.position - transform.position; 21 // 求两个向量的夹角 22 float jiajiao = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg; 23 if (distance < SkillDistance) 24 { 25 // 绘制扇形区域(绘制时取消注释) 26 //ToDrawSectorSolid(transform, transform.localPosition, SkillJiaodu, SkillDistance); 27 28 if (jiajiao <= SkillJiaodu * 0.5f) 29 { 30 Debug.Log("小球出现在扇形范围内!"); 31 } 32 } 33 } 34 }
计算并绘制扇形范围区域,便于测试和观察
1 GameObject go; 2 MeshFilter mf; 3 MeshRenderer mr; 4 Shader shader; 5 6 // 绘制实心扇形 7 public void ToDrawSectorSolid(Transform t, Vector3 center, float angle, float radius) 8 { 9 int pointAmmount = 100; 10 float eachAngle = angle / pointAmmount; 11 12 Vector3 forward = t.forward; 13 List<Vector3> vertices = new List<Vector3>(); 14 15 vertices.Add(center); 16 for (int i = 0; i < pointAmmount; i++) 17 { 18 Vector3 pos = Quaternion.Euler(0f, -angle / 2 + eachAngle * (i - 1), 0f) * forward * radius + center; 19 vertices.Add(pos); 20 } 21 CreateMesh(vertices); 22 } 23 24 // 创建网格 25 private GameObject CreateMesh(List<Vector3> vertices) 26 { 27 int[] triangles; 28 Mesh mesh = new Mesh(); 29 30 int triangleAmount = vertices.Count - 2; 31 triangles = new int[3 * triangleAmount]; 32 33 // 根据三角形的个数,来计算绘制三角形的顶点顺序 34 for (int i = 0; i < triangleAmount; i++) 35 { 36 triangles[3 * i] = 0; 37 triangles[3 * i + 1] = i + 1; 38 triangles[3 * i + 2] = i + 2; 39 } 40 41 if (go == null) 42 { 43 go = new GameObject("mesh"); 44 go.transform.position = new Vector3(0f, 0.1f, 0.5f); 45 46 mf = go.AddComponent<MeshFilter>(); 47 mr = go.AddComponent<MeshRenderer>(); 48 49 shader = Shader.Find("Unlit/Color"); 50 } 51 52 mesh.vertices = vertices.ToArray(); 53 mesh.triangles = triangles; 54 55 mf.mesh = mesh; 56 mr.material.shader = shader; 57 mr.material.color = Color.red; 58 59 return go; 60 }
具体效果:
二、长方形攻击范围检测
1 using System.Collections.Generic; 2 using UnityEngine; 3 4 public class AttackRange : MonoBehaviour 5 { 6 // 攻击目标 7 public Transform Target; 8 9 private void Update() 10 { 11 // 计算玩家与敌人的距离 12 float distance = Vector3.Distance(transform.position, Target.position); 13 // 玩家与敌人的方向向量 14 Vector3 temVec = Target.position - transform.position; 15 // 与玩家正前方做点积 16 float forwardDistance = Vector3.Dot(temVec, transform.forward.normalized); 17 if (forwardDistance > 0 && forwardDistance <= 10) 18 { 19 // 绘制矩形区域(绘制时取消注释) 20 //ToDrawRectangleSolid(transform, transform.localPosition, 10, 2); 21 22 float rightDistance = Vector3.Dot(temVec, transform.right.normalized); 23 24 if (Mathf.Abs(rightDistance) <= 3) 25 { 26 Debug.Log("小球进入矩形攻击范围"); 27 } 28 } 29 } 30 }
计算并绘制矩形范围区域,便于测试和观察
1 GameObject go; 2 MeshFilter mf; 3 MeshRenderer mr; 4 Shader shader; 5 6 // 绘制矩形区域 7 public void ToDrawRectangleSolid(Transform t, Vector3 bottomMiddle, float length, float width) 8 { 9 List<Vector3> vertices = new List<Vector3>(); 10 11 vertices.Add(bottomMiddle - t.right * width); 12 vertices.Add(bottomMiddle - t.right * width + t.forward * length); 13 vertices.Add(bottomMiddle + t.right * width + t.forward * length); 14 vertices.Add(bottomMiddle + t.right * width); 15 16 CreateMesh(vertices); 17 } 18 19 // 创建网格 20 private GameObject CreateMesh(List<Vector3> vertices) 21 { 22 int[] triangles; 23 Mesh mesh = new Mesh(); 24 25 int triangleAmount = vertices.Count - 2; 26 triangles = new int[3 * triangleAmount]; 27 28 for (int i = 0; i < triangleAmount; i++) 29 { 30 triangles[3 * 1] = 0; 31 triangles[3 * i + 1] = i + 1; 32 triangles[3 * i + 2] = i + 2; 33 } 34 35 if (go == null) 36 { 37 go = new GameObject("Rectang"); 38 go.transform.position = new Vector3(0, 0.1f, 0); 39 mf = go.AddComponent<MeshFilter>(); 40 mr = go.AddComponent<MeshRenderer>(); 41 42 shader = Shader.Find("Unlit/Color"); 43 } 44 45 mesh.vertices = vertices.ToArray(); 46 mesh.triangles = triangles; 47 mf.mesh = mesh; 48 mr.material.shader = shader; 49 mr.material.color = Color.red; 50 51 return go; 52 }
具体效果:
三、半圆形攻击范围检测
1 using System.Collections.Generic; 2 using UnityEngine; 3 4 public class AttackRange : MonoBehaviour 5 { 6 // 攻击目标 7 public Transform Target; 8 9 private void Update() 10 { 11 // 计算玩家与敌人的距离 12 float distance = Vector3.Distance(transform.position, Target.position); 13 // 玩家与敌人的方向向量 14 Vector3 temVec = Target.position - transform.position; 15 // 与玩家正前方做点积 16 float forwardDistance = Vector3.Dot(temVec, transform.forward.normalized); 17 if (forwardDistance > 0 && forwardDistance <= 10) 18 { 19 // 绘制半圆区域(绘制时取消注释) 20 //ToDrawSectorSolid(transform, transform.localPosition, 180, 4); 21 if (distance <= 5) 22 { 23 Debug.Log("小球进入半圆攻击范围!"); 24 } 25 } 26 } 27 }
计算并绘制半圆范围区域,便于测试和观察
1 GameObject go; 2 MeshFilter mf; 3 MeshRenderer mr; 4 Shader shader; 5 6 // 绘制半圆区域 7 public void ToDrawSectorSolid(Transform t, Vector3 center, float angle, float radius) 8 { 9 int pointAmmount = 100; 10 float eachAngle = angle / pointAmmount; 11 12 Vector3 forward = t.forward; 13 List<Vector3> vertices = new List<Vector3>(); 14 15 vertices.Add(center); 16 for (int i = 0; i < pointAmmount; i++) 17 { 18 Vector3 pos = Quaternion.Euler(0f, -angle / 2 + eachAngle * (i - 1), 0f) * forward * radius + center; 19 vertices.Add(pos); 20 } 21 CreateMesh(vertices); 22 } 23 24 // 创建网格 25 private GameObject CreateMesh(List<Vector3> vertices) 26 { 27 int[] triangles; 28 Mesh mesh = new Mesh(); 29 30 int triangleAmount = vertices.Count - 2; 31 triangles = new int[3 * triangleAmount]; 32 33 // 根据三角形的个数,来计算绘制三角形的顶点顺序 34 for (int i = 0; i < triangleAmount; i++) 35 { 36 triangles[3 * i] = 0; 37 triangles[3 * i + 1] = i + 1; 38 triangles[3 * i + 2] = i + 2; 39 } 40 41 if (go == null) 42 { 43 go = new GameObject("mesh"); 44 go.transform.position = new Vector3(0f, 0.1f, 0.5f); 45 46 mf = go.AddComponent<MeshFilter>(); 47 mr = go.AddComponent<MeshRenderer>(); 48 49 shader = Shader.Find("Unlit/Color"); 50 } 51 52 mesh.vertices = vertices.ToArray(); 53 mesh.triangles = triangles; 54 55 mf.mesh = mesh; 56 mr.material.shader = shader; 57 mr.material.color = Color.red; 58 59 return go; 60 }
具体效果:
*** | 以上内容仅为学习参考、学习笔记使用 | ***