gpt4 book ai didi

c# - 防止移动到最近的可能位置

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:03 27 4
gpt4 key购买 nike

enter image description here

正如您在上图中看到的,当我点击白色路径时,对象完美地移向点击的位置。当我点击蓝色地面时,对象不会移动到那里,但它会在白色路径上找到最近的可能位置,这是我不想要的行为。

如果点击在白色路径之外,我希望对象不移动。

检查员:

白色路径:静态导航 - 可步行

蓝色地面:无。

对象脚本:

void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}

最佳答案

I want the object to not move if the click is outside of the white path.

您可以通过检查单击了哪个对象来做到这一点。您可以使用 hit.collider.name 按名称检查它,或者您可以使用带有 hit.collider.CompareTag 的标签来查看单击了哪个对象。我建议你使用标签。

创建一个名为“whitepath”的标签,然后将您的 whitepath GameObject 设置为该标签。然后您可以在光线转换后比较标签名称。 This是关于如何创建标签的 Unity。

void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
//Check for white path
if (hit.collider.CompareTag("whitepath"))
{
navAgent.destination = hit.point;
navAgent.Resume();
}
}
}
}

关于c# - 防止移动到最近的可能位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42774426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com