怎么使用Unity相机确定AI代理的视线范围,求大神

 我来答
yi...3@163.com
2014-05-11
知道答主
回答量:7
采纳率:0%
帮助的人:7.5万
展开全部
Unity提供了很多方式来分析游戏世界。它有像Physics.RayCast(…)或Physics.OverlapSphere(…) 这样的函数,它也给我们提供关于边界,网格和渲染器的信息。对我的项目来说,人工智能代理必须能够寻找目的地,可以访问所有他们看到和感兴趣对象。

现在我们有一个函数告诉我们一个特定的对象是否在人工智能的视线内,我只是测试每个对象,由一个相机位置为原点,以相机远平面的距离为半径进行重叠范围检查。我为每个对象调用之前的SeeGameObject函数并将在视线内的对象保存在一个数组中。

public bool SeeGameObject(GameObject go)

{

// if object has a renderer and visible by any camera and is in this camera frustum

if(go.renderer != null && go.renderer.isVisible && GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(_camera), go.renderer.bounds))

{

RaycastHit hitInfo;

// by default we use the rough renderer bounds

Vector3 center = go.renderer.bounds.center;

Vector3 extents = go.renderer.bounds.extents;

float coefreduc = 0.8f;

Vector3[] gobounds; // points to check for linecast from camera

MeshFilter meshfilter = go.GetComponent<MeshFilter>();

if(meshfilter != null) // Almost every interesting game object that is render has a mesh

{

center = go.transform.position;

extents = meshfilter.mesh.bounds.extents;

extents.Scale(go.transform.lossyScale);

gobounds = new Vector3[33]{ // We can add more or remove some, it increase precision for not too much time or memory cost

Vector3.zero,

go.transform.TransformDirection(new Vector3(extents.x,extents.y,extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,-extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,-extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,-extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,-extents.z)*0.9f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,-extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,-extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,-extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,-extents.z)*0.5f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,-extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,-extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,-extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,-extents.z)*0.75f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(extents.x,extents.y,-extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(extents.x,-extents.y,-extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(-extents.x,extents.y,-extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,extents.z)*0.25f),

go.transform.TransformDirection(new Vector3(-extents.x,-extents.y,-extents.z)*0.25f)

};

}

else // Only if gameobject has no mesh (= almost never) (Very approximately checking points using the renderer bounds and not the mesh bounds)

{

gobounds = new Vector3[9]{

Vector3.zero,

new Vector3(extents.x,extents.y,extents.z)*coefreduc,

new Vector3(extents.x,extents.y,-extents.z)*coefreduc,

new Vector3(extents.x,-extents.y,extents.z)*coefreduc,

new Vector3(extents.x,-extents.y,-extents.z)*coefreduc,

new Vector3(-extents.x,extents.y,extents.z)*coefreduc,

new Vector3(-extents.x,extents.y,-extents.z)*coefreduc,

new Vector3(-extents.x,-extents.y,extents.z)*coefreduc,

new Vector3(-extents.x,-extents.y,-extents.z)*coefreduc

};

}

foreach(Vector3 v in gobounds)

{

// test if it can see gameobject

if(GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(_camera), new Bounds(v+center, Vector3.zero)) // if point in viewing frustrum

&& (!Physics.Linecast(transform.position, v+center, out hitInfo) || hitInfo.collider.gameObject == go )) // if nothing between viewing position and point

{

if(graphicalDebug)

{

Debug.DrawLine(transform.position, v+center,Color.red, 0.01f, false);

}

return true;

}

}

}

return false;

}
复制代码

你可以搜索编@程=回=忆=录=之=unity3d学习一下,希望对你的问题有所帮助
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式