コード例 #1
0
    public GameObject Detection()
    {
        GameObject player     = null;
        float      startAngle = -(angle / 2);

        var        offset = Quaternion.AngleAxis(-3 * rays / 2, Vector3.up);
        RaycastHit hit;
        var        step     = Quaternion.AngleAxis(3, Vector3.up);
        var        rotation = transform.rotation;
        var        origin   = originPos.transform.position;
        Vector3    direction;

        List <Vector3> meshDetection = new List <Vector3> {
            new Vector3(0, 0, 0)
        };

        for (int i = 0; i < rays; i++)
        {
            float x = Mathf.Sin(Mathf.Deg2Rad * startAngle) * GetRange();
            float z = Mathf.Cos(Mathf.Deg2Rad * startAngle) * GetRange();

            direction = rotation * new Vector3(x, 0, z);

            if (Physics.Raycast(origin, direction.normalized, out hit, GetRange()))
            {
                Vector3 cHit = (hit.point - origin);

                meshDetection.Add(hit.point - origin);

                if (hit.collider.CompareTag("Entity"))
                {
                    player = hit.collider.gameObject;
                }
            }
            else
            {
                meshDetection.Add(direction);
            }
            startAngle += angle / rays;
        }

        fOv.GetComponent <FOV>().SetOrigin(origin);
        fOv.GetComponent <FOV>().SetVertices(meshDetection.ToArray());
        return(player);
    }