码迷,mamicode.com
首页 > 其他好文 > 详细

判断对象是否在视线内

时间:2014-05-19 07:40:39      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   c   

bubuko.com,布布扣
// Cast a sphere with the desired distance. Check each collider hit to see if it is within the field of view. Set objectFound
    // to the object that is most directly in front of the agent
    /// <summary>
    /// Withins the sight.
    /// </summary>
    /// <returns>The sight.</returns>
    /// <param name="transform">玩家</param>
    /// <param name="fieldOfViewAngle">可视角度</param>
    /// <param name="viewDistance">可视距离</param>
    /// <param name="objectLayerMask">对象层</param>
    public static Transform WithinSight(Transform transform, float fieldOfViewAngle, float viewDistance, LayerMask objectLayerMask)
    {
        Transform objectFound = null;
        var hitColliders = Physics.OverlapSphere(transform.position, viewDistance, objectLayerMask);//获取可视范围内的对象
        if (hitColliders != null) {
            float minAngle = Mathf.Infinity;
            float angle = 0;
            for (int i = 0; i < hitColliders.Length; ++i) {
                // The hit agent needs to be within the field of view of the current agent
                if ((angle = Vector3.Angle(transform.position, hitColliders[i].transform.position)) < fieldOfViewAngle) {//获取可视角度内的对象
                    RaycastHit hit;
                    // The hit agent needs to be within view of the current agent
                    if (Physics.Linecast(transform.position, hitColliders[i].transform.position, out hit)) {//检测玩家与对象之间是否存在障碍物
                        if (hit.transform.Equals(hitColliders[i].transform)) {
                            // This agent is within sight. Set it to the agentInSight GameObject if the angle is less than any of the other agents
                            if (angle < minAngle) {//获取最小角度对象
                                minAngle = angle;
                                objectFound = hitColliders[i].transform;
                            }
                        }
                    }
                }
            }
        }
        return objectFound;
    }
bubuko.com,布布扣

 

判断对象是否在视线内,布布扣,bubuko.com

判断对象是否在视线内

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/88999660/p/3732286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!