获得最近的可能目的地 - Unity3d
Getting closest possible destination - Unity3d
我正在尝试了解如何在导航网格障碍物挡道时使用 Unity 的导航系统最接近目的地。
我有这个示例,其中包含一个 Capsule 障碍物(启用 Carve)和一个使用脚本管理的 capsule agent。它似乎工作正常,但是当我 "click" 在障碍物上设置代理的目的地(到雕刻区域内的一个点)时,代理移动到障碍物周围的另一个位置。
如何让智能体到达障碍物周围最近的点或到所选目的地最近的点(即在障碍物区域内)?
移动代理的脚本
using UnityEngine;
using UnityEngine.AI;
public class CapsuleMovement : MonoBehaviour {
NavMeshAgent agent;
public NavMeshPathStatus partial;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
agent.destination = hit.point;
}
}
}
}
将被点击对象的中心位置稍微向移动方向unit/player移动,然后用那个位置作为你的坐标,而不是直接鼠标点击。
如果这不起作用。
这不是一个有保证的答案,因为在这种情况下很难给出答案,但我希望这能让你更进一步。
我怀疑它可能是你点击的坐标,它不正常。尝试在鼠标单击的位置生成一个 "ClickObject",它可以只是一个彩色球体。这样您就可以确认点击实际发生的位置。
这里还有 2 种方法,在使用 NavMeshes 和定位时可能会派上用场。
您可以尝试使用 SamplePosition。
https://docs.unity3d.com/540/Documentation/ScriptReference/NavMesh.SamplePosition.html
Finds the closest point on NavMesh within specified range.
也许还有 FindClosestEdge
https://docs.unity3d.com/530/Documentation/ScriptReference/NavMesh.FindClosestEdge.html
Locate the closest NavMesh edge from a point on the NavMesh.
我正在尝试了解如何在导航网格障碍物挡道时使用 Unity 的导航系统最接近目的地。 我有这个示例,其中包含一个 Capsule 障碍物(启用 Carve)和一个使用脚本管理的 capsule agent。它似乎工作正常,但是当我 "click" 在障碍物上设置代理的目的地(到雕刻区域内的一个点)时,代理移动到障碍物周围的另一个位置。
如何让智能体到达障碍物周围最近的点或到所选目的地最近的点(即在障碍物区域内)?
移动代理的脚本
using UnityEngine;
using UnityEngine.AI;
public class CapsuleMovement : MonoBehaviour {
NavMeshAgent agent;
public NavMeshPathStatus partial;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
agent.destination = hit.point;
}
}
}
}
将被点击对象的中心位置稍微向移动方向unit/player移动,然后用那个位置作为你的坐标,而不是直接鼠标点击。
如果这不起作用。
这不是一个有保证的答案,因为在这种情况下很难给出答案,但我希望这能让你更进一步。
我怀疑它可能是你点击的坐标,它不正常。尝试在鼠标单击的位置生成一个 "ClickObject",它可以只是一个彩色球体。这样您就可以确认点击实际发生的位置。
这里还有 2 种方法,在使用 NavMeshes 和定位时可能会派上用场。
您可以尝试使用 SamplePosition。
https://docs.unity3d.com/540/Documentation/ScriptReference/NavMesh.SamplePosition.html
Finds the closest point on NavMesh within specified range.
也许还有 FindClosestEdge
https://docs.unity3d.com/530/Documentation/ScriptReference/NavMesh.FindClosestEdge.html
Locate the closest NavMesh edge from a point on the NavMesh.