c# - Physics2D.Raycast 返回 null

标签 c# unity3d

我有一个问题让我在这里非常难过。我将从 Key codes 转到 mouse input - 然后它后来变成触摸输入,但首先我想弄清楚为什么我不能只用鼠标来做到这一点。

我有一个 raycast2d 设置 - 我希望 raycast 读取与屏幕上我的对象的碰撞。他们只会在它的对象被标记为“猫”时使用react - 基本上一旦发生这种情况,猫就会滑出并试图攻击。但是,它告诉我 tagg 本身是一个实例化的引用。但是默认情况下对象本身存在,所以我不确定在这里做什么。这是我的整个脚本。

 void Update() {
    //if ((Input.GetKeyDown(KeyCode.O) && !attacking && attackTimer <= 0)) {
    if (Input.GetMouseButtonDown(0) && !attacking && attackTimer <= 0) {  //every frame check to see if the mouse has been clicked.
                                                                          //Get the mouse position on the screen and send a raycast into the game world from that position.
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Vector 2 means only 2 points of axis are read. worldpoint means check the point of the world. Camera is used to determien what we are looking at, and we fill it with our mouse location.
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
        if (hit.collider == null) {//THIS IS THE LINE that says it is having issues. If i REMOVE this line, ANYWHERE i click on the screen activates the cat and thats not what I want to happen.

            Debug.Log("No Object Touched");

            if (hit.collider.CompareTag("Cat")) {
                GameManager.Instance.AudioSource.PlayOneShot(SoundManager.Instance.Swipe);

                attacking = true;
                attackTimer = attackCd;

                attackTrigger.enabled = true;
            }

更新代码以匹配请求的更改: 我现在得到的错误是 NullreferenceException - 对于:

 if (hit.collider.CompareTag("Cat")) {

在使用程序员推荐的测试方法重新测试后,这与我之前遇到的错误相同。

控制台将显示我没有单击对象,然后显示空值。所以我想它是想告诉我它没有找到场景中存在的任何标记为猫的东西?尽管 Cat 是添加的自定义标签,并且它已应用于作为猫的游戏对象 - 使用 Box collider。它是否需要 Material 或任何东西来阅读它的存在?是否有另一种方法可以通过单击其特定位置来调用此对象?

更新:

       Debug.Log("No Object Touched");
                if (hit.collider) {
                    if (hit.collider.tag == "cat1") { - 

这摆脱了空引用,但它根本不读猫。如果我点击猫什么也不会发生。是的,它现在在编辑器中被正确标记为“cat1”。 meanign 标签 - 新的自定义标签,创建的 cat1 标签。转到游戏对象,将标签更改为 cat1。还确保对撞机已打开,并带有触发器。

最佳答案

经过几个小时的搜索,我终于找到了空值的原因。

您需要将“New Component”>“Physics”>“Box Collider”附加到您的 sprite 游戏对象。

现在您可以在添加到游戏对象的脚本中使用以下代码片段来了解对象是否被点击:

private Ray ray;
private RaycastHit hit;

private void Update()
{
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
    {
        ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            if (hit.transform.gameObject.name == "YourGameObject")
            {
                // Do your stuff on click
            }
        }
    }
}

这就是 3d 对象可以轻松检测到触摸的原因,因为在创建它们时它们已经附加了碰撞器。

希望这个回答能帮到别人。

关于c# - Physics2D.Raycast 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788004/

相关文章:

c# - 如何获得从对象到空间映射的距离

android - Facebook Analytics for Apps 中未跟踪 Android 上的 Unity 游戏 Activity 安装

c# - 检测图片框上的鼠标拖动的事件处理程序(winforms,c#)

c# - .NET 程序集绑定(bind),我可以将一个程序集映射到另一个程序集中的版本吗?

c# - 正则表达式提取电话号码

c# - 实体数据栏和数据栏最小值的手动版本和编码版本之间的外观不一致

android - Unity3D-无法构建64位Android

c# - 如何覆盖 NullReferenceException 行为以获得更好的信息量更大的错误?

c# - 如何识别多边形是wpf中的三角形

c# - 让玩家在 Unity3D 中正确跳跃