人物角色群体攻击判定四(三角区域判断)
我们在游戏一些逃生游戏中很可能让主角不能在怪物前方的三角区域内, 如果存在里面玩家就会被发现. 这种判断的该如何实现呢?
请看下图: (此图是主角探索怪物)
using UnityEngine; using System.Collections; using UnityEditor; using System; public class Test : MonoBehaviour { public Transform a; public Transform b; public Transform c; public Transform d; public TextMesh 正角; public TextMesh 补角; public TextMesh distanceText; public TextMesh angelText; public TextMesh isInEayText; public TextMesh dcDistanceText; public float distance; public float angle; // Update is called once per frame void Update () { Vector3 ac = a.position - c.position; Vector3 bc = b.position - c.position; Vector3 cb = c.position - b.position; //计算cd的距离 float dcDistance = Mathf.Abs(Vector3.Distance(d.position, c.position)); dcDistanceText.text = "DC的距离: " + dcDistance; /* * cos<A,B>=(ac+bd)/(根号a*a+b*b)(根号c*c+d*d) * 两向量夹角余弦等于向量数量积除以两向量模的乘积 */ double hudu = Math.Acos( (ac.x * bc.x + ac.y * bc.y) / (Mathf.Sqrt(ac.x * ac.x + ac.y * ac.y) * Mathf.Sqrt(bc.x * bc.x + bc.y * bc.y))); //弧度转换角度 double jiajiao = hudu / Math.PI * 180; 正角.text = jiajiao + "度"; 补角.text = (180 - jiajiao) + "度"; distanceText.text = "玩家看得到的距离: " + distance; angelText.text = "玩家看得到的角度: " + angle; //判断敌人是否在视线当中 if (jiajiao >= 0 && jiajiao <= angle && dcDistance <= distance) { isInEayText.text = "敌人是否在玩家视线内: true"; } else { isInEayText.text = "敌人是否在玩家视线内: false"; } } //绘制线段 public void OnDrawGizmos() { Gizmos.DrawLine(a.position, b.position); Gizmos.DrawLine(a.position, c.position); Gizmos.DrawLine(b.position, c.position); Gizmos.DrawLine(b.position, d.position); Gizmos.DrawLine(a.position, d.position); } }
项目下载地址: http://yunpan.cn/cdPpwnbrja5xA 访问密码 4146
如果你感兴趣,你可以把你妹妹介绍给我