zoukankan      html  css  js  c++  java
  • 判断对象是否在视线内

    // 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 transformfloat fieldOfViewAnglefloat viewDistanceLayerMask objectLayerMask)
        {
            Transform objectFound = null;
            var hitColliders = Physics.OverlapSphere(transform.positionviewDistanceobjectLayerMask);
            if (hitColliders != null) {
                float minAngle = Mathf.Infinity;
                float angle = 0;
                Debug.Log(hitColliders.Length);
                for (int i = 0i < hitColliders.Length; ++i) {
                    // The hit agent needs to be within the field of view of the current agent
                     angle = Vector3.AnglehitColliders[i].transform.position - transform.position , transform.forward);

                    if (angle < fieldOfViewAngle) {
                        RaycastHit hit;
                        // The hit agent needs to be within view of the current agent

                        Vector3  position = new Vector3(transform.position.x,1f,transform.position.z);
                        Vector3 targetPosition = new Vector3(hitColliders[i].transform.position.x,1f,hitColliders[i].transform.position.z);
                        Debug.DrawLine(positiontargetPosition - position);

                        if(Physics.Raycast(positiontargetPosition - positionout hit)){
                            if (hit.transform.Equals(hitColliders[i].transform)) {
                                // This agent is within sightSet 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;
        }
  • 相关阅读:
    利用print2flashsetup.exe文档转swf
    Linux 脚本内容指定用户执行
    第一讲:网络协议概述
    第三讲:ifconfig:最熟悉又陌生的命令行
    第2讲 | 网络分层的真实含义是什么?
    Fiddler -工具使用介绍(附:拦截请求并修改返回数据)(转)
    Fiddler 抓包工具总结(转)
    网络抓包wireshark(转)
    Axure RP 授权码
    第6堂视频课:看到词句就会读-下
  • 原文地址:https://www.cnblogs.com/88999660/p/3732286.html
Copyright © 2011-2022 走看看