【问题标题】:Message not being disabled when not in collider不在对撞机中时未禁用消息
【发布时间】:2021-01-14 23:13:42
【问题描述】:

当玩家与 NPC 对撞机发生碰撞并且玩家未与 NPC 发生碰撞时,该消息被禁用时,我试图显示一条消息“按 E 与 NPC 交谈”。该消息确实会在碰撞时出现,但在没有碰撞时它不会被禁用我尝试了很多东西但似乎没有任何效果。任何人都可以帮忙吗?这是我的代码和我尝试过的一些事情:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Task_7 : MonoBehaviour
{
   public GameObject PressEmsg;
   //public bool isNearNPC = true;
  // Start is called before the first frame update
  void Start()
 {
    PressEmsg.gameObject.SetActive(false);
}

// Update is called once per frame
void Update()
{
    Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, 5f);

    //bool isNearNPC = false;
    //we are looping in the array hitColliders
    foreach(Collider collider in nearbyColliders)
    {
        if(collider.gameObject.tag == "NPC")
        {
            PressEmsg.gameObject.SetActive(true);
            print("NPC DETECTED");
            //isNearNPC = true;
        }

        else
        {
            PressEmsg.gameObject.SetActive(false);
            print("NPC NOT DETECTED");
        }
        /*
        else if(collider.gameObject.tag != "NPC")
        {
            PressEmsg.gameObject.SetActive(false);
            print("NPC NOT DETECTED");
        }
        */
        
    }   

    /*foreach(Collider collider1 in notnearbyColliders)
    {
        if(collider1.gameObject.tag != "NPC")
        {
            PressEmsg.gameObject.SetActive(false);
            print("NPC NOT DETECTED");
        }
    }
    */
   
    
}


}

【问题讨论】:

    标签: c# unity3d collider mesh-collider


    【解决方案1】:

    如果您完全没有冲突,您的循环将不会有任何迭代。因此不会发生停用,除非您附近有一个没有标签NPC 的对象。

    进一步您将遍历所有附近的对象并检查每个对象是否具有标签NPC。所以循环完全取决于对撞机迭代的顺序。它可能例如碰巧你第一次有一个带有标签的点击,然后你有第二个没有标签的点击=>你再次错误地停用了对象。

    你应该使用Linq Any,例如

    using System.Linq;
    
    ...
    
    void Update()
    {
        var nearbyColliders = Physics.OverlapSphere(transform.position, 5f);
    
        // This will be true if any of the nearbyColliders has the tag "NPC"
        // If there are no colliders this will automatically be false accordingly
        var detected = nearbyColliders.Any(collider => collider.CompareTag("NPC"));
        // Basically this equals somewhat doing 
        //var detected = false;
        //foreach(var collider in nearbyColliders)
        //{
        //    if(collider.CompareTag("NPC"))
        //    {
        //        detected = true;
        //        break;
        //    }
        //}
    
        PressEmsg.gameObject.SetActive(detected);
        print(detected ? "NPC detected" : "NPC not detected");
    }
    

    一般出于性能原因,请避免登录Update!即使您的用户看不到日志,它仍然可以完成并且非常昂贵。


    注意:在智能手机上输入,但我希望思路清晰

    【讨论】:

    • 我很想使用你的代码,但是它在一些语法下面给了我红线
    • @Z.Mizzi 哪个? ;) 我是在智能手机上输入的,所以可能存在一些语法问题
    • 哦啊啊啊那一行:var detected = nearColliders.Any(collider => collider.CompareTag("NPC"); 'var' 和分号下方有一条红线
    • 您在 Unity 控制台中是否遇到任何错误?哦,在"NPC") 之后缺少一个)
    【解决方案2】:

    看起来如果你没有任何冲突,你就不会进入你的 for 循环。

    在遍历找到的对撞机之前,我会默认消息是不活动的,但我会使用一个变量,所以我实际上只调用了一次消息状态方法:

    bool isNearNpc = false;
    
    Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, 5f);
    
    foreach(Collider collider in nearbyColliders)
    {
        if(collider.gameObject.tag == "NPC")
        {
            print("NPC DETECTED");
            isNearNpc = true;
        }
    }
    
    
    PressEmsg.gameObject.SetActive(isMessageActive);
    print($"NPC DETECTED: { isNearNpc }");
    

    【讨论】:

    • 我尝试了您的代码,但文本似乎没有在播放时停用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多