文章出處

// 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;
    }

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()