赞
踩
代码如下:
- using UnityEngine;
-
- public class RangeCheck
- {
- //--扇形
- public static bool CurveRange(Transform self, Transform target, float maxDistance, float maxAngle)
- {
- return CurveRange(self, target, 0, maxDistance, maxAngle);
- }
- public static bool CurveRange(Transform self, Transform target, float minDistance, float maxDistance, float maxAngle)
- {
- Vector3 playerDir = self.forward;
- Vector3 enemydir = (target.position - self.position).normalized;
- float angle = Vector3.Angle(playerDir, enemydir);
- if (angle > maxAngle * 0.5f)
- {
- return false;
- }
- float distance = Vector3.Distance(target.position, self.position);
- if (distance <= maxDistance && distance >= minDistance)
- {
- return true;
- }
- return false;
- }
- //--圆形
- public static bool CircleRange(Transform self, Transform target, float maxDistance)
- {
- return CircleRange(self, target, 0, maxDistance);
- }
- public static bool CircleRange(Transform self, Transform target, float minDistance, float maxDistance)
- {
- float distance = Vector3.Distance(target.position, self.position);
- if (distance <= maxDistance && distance >= minDistance)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //--矩形
- public static bool SquareRange(Transform self, Transform target, float maxWidth, float maxHeight)
- {
- return SquareRange(self, target, maxWidth, 0, maxHeight);
- }
- public static bool SquareRange(Transform self, Transform target, float maxWidth, float minHeight, float maxHeight)
- {
- Vector3 enemyDir = (target.position - self.position).normalized;
- float angle = Vector3.Angle(enemyDir, self.forward);
- if (angle > 90)
- {
- return false;
- }
- float distance = Vector3.Distance(target.position, self.position);
- float z = distance * Mathf.Cos(angle * Mathf.Deg2Rad);
- float x = distance * Mathf.Sin(angle * Mathf.Deg2Rad);
- if (x <= maxWidth * 0.5f && z <= maxHeight && z >= minHeight)
- {
- return true;
- }
- return false;
- }
- //--等腰三角形
- public static bool TriangleRange(Transform self, Transform target, float maxDistance, float maxAngle)
- {
- Vector3 playerDir = self.forward;
- Vector3 enemydir = (target.position - self.position).normalized;
- float angle = Vector3.Angle(playerDir, enemydir);
- if (angle > maxAngle * 0.5f)
- {
- return false;
- }
- float angleDistance = maxDistance * Mathf.Cos(maxAngle * 0.5f * Mathf.Deg2Rad) / Mathf.Cos(angle * Mathf.Deg2Rad);
- float distance = Vector3.Distance(target.position, self.position);
- if (distance <= angleDistance)
- {
- return true;
- }
- return false;
- }
- }
应用代码如下:
- using UnityEngine;
-
- public class RangeTest : MonoBehaviour
- {
- private GameObject cube1;
- private GameObject cube2;
- private void Start()
- {
- cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- cube1.transform.position = Vector3.zero;
- cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- cube2.transform.position = new Vector3(0, 0, 4);
-
- Debug.Log(cube1.transform.position.ToString());
- Debug.Log(cube2.transform.position.ToString());
-
- bool isInCurveRange = RangeCheck.CurveRange(cube1.transform, cube2.transform, 3.9f, 90);
- Debug.Log(isInCurveRange);
-
- bool isInCircleRange = RangeCheck.CircleRange(cube1.transform, cube2.transform, 4.1f, 5);
- Debug.Log(isInCircleRange);
-
- bool isInSquareRange = RangeCheck.SquareRange(cube1.transform, cube2.transform, 5, 4.1f, 10);
- Debug.Log(isInSquareRange);
-
- bool isInTriangleRange = RangeCheck.TriangleRange(cube1.transform, cube2.transform, 5, 60);
- Debug.Log(isInTriangleRange);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。