Unity - OnTriggerEnter 未注册碰撞 [英] Unity - OnTriggerEnter not registering collision

查看:35
本文介绍了Unity - OnTriggerEnter 未注册碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 2D 角色和对撞机对象进行碰撞.我已经定义了 OnTriggerEnter 函数以在输入触发器时在调试器中显示一条消息.该角色是来自 Unity 标准资产包(包含 BoxCollider2D)的CharacterRobotBoy"资产,我希望它与另一个带有 BoxCollider2D 的对象发生碰撞,设置为触发器.我在第二个对象上触发了勾选.

I'm trying to do collision for a 2D character and a collider object. I have defined the OnTriggerEnter function to display a message in the debugger when a trigger is entered. The character is the "CharacterRobotBoy" asset from the Unity standard assets package (contains a BoxCollider2D) and I want it to collide with another object with attached BoxCollider2D, set as a trigger. I have trigger ticked on the second object.

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

public class PickUpCheck : MonoBehaviour {


    private int pickUpCount;

    // Use this for initialization
    void Start () {
        pickUpCount = 0;
    }


    void OnTriggerEnter(Collider collider)
    {
       // if (collider.gameObject.name == "RobotBoy")
       //     pickUpCount++;
        Debug.Log("PickUp " + pickUpCount);
    }

    // Update is called once per frame

    void Update () {

    }
}

我尝试将脚本附加到角色和其他对象上,但似乎无法记录与触发器的碰撞.

I've tried attaching the script to both the character and the other object but can't seem to register collisions with the trigger.

/edit - 我读过有或曾经是 OnTriggerEnter2D.我试过调用它,但它在 Visual Studio 中无法识别.不确定它是否仍然存在或者我做错了什么?

/edit - I've read there is or was an OnTriggerEnter2D. I've tried calling this but it's not recognized in Visual Studio. Not sure if it still exists or I'm doing something wrong?

/edit - 将代码切换为 -

/edit - Switched code to -

void OnTriggerEnter2D(Collider2D collider)
{
   // if (collider.gameObject.name == "RobotBoy")
   //     pickUpCount++;
    Debug.Log("PickUp " + pickUpCount);
}

但仍然没有运气...

拾取对象

/edit - @Eddge 为角色和拾取对象设置了一个公共层,尽管我认为在没有设置层的情况下仍然会发生碰撞?

/edit - @Eddge Have set a common layer for both the character and pickup object, though I think collisions should still occur with no layers set?

碰撞矩阵

这个 回答内容丰富,但我确保我的设置中已经包含了所描述的组件 - 两个对象上的碰撞器,其中一个对象上存在刚体,一个设置为触发器.

This answer was informative, but I made sure I have the described components in my set up already - colliders on both objects, rigidbody present on one of the objects and one set as trigger.

/edit 解决了! - 好的,所以我清理了调试(感谢 @Edge 的建议).我将脚本移到角色上并注意到发生了碰撞,但与拾取对象没有发生碰撞.

/edit Solved! - Ok, so I cleaned up the debug (thanks to @Eddge for the suggestion). I moved the script to the character and noticed that collisions were occurring but not with the pickup object.

我切换了 OnTriggerEnter2D 中的代码以输出碰撞对象的名称,这有助于我更清楚地了解发生了什么:

I switched the code in the OnTriggerEnter2D to output the name of the collided objects and this helped me get a clearer idea of what was going on:

void OnTriggerEnter2D(Collider2D collider)
{
    Debug.Log(collider.gameObject.name);
}

事实证明问题出在我修改为拾音器的预制件中的 BoxCollider2D.我重建了游戏对象,这解决了问题.

Turns out the problem was with the BoxCollider2D in the prefab I modified to be the pickup. I rebuilt the game object and this solved the problem.

推荐答案

如果您正在开发 2D 游戏,则必须使用 OnTriggerEnter2D(Collider2D other).目前,您正在使用 OnTriggerEnter,它将仅注册 3D 碰撞.另外,请确保您将 Collider2D 作为函数的参数传递.

If you're working on a 2D game, you must use OnTriggerEnter2D(Collider2D other). Currently, you're using OnTriggerEnter which will only register 3D collisions. Also, make sure you're passing a Collider2D as the function's parameter.

把你的代码改成这样:

 void OnTriggerEnter2D(Collider2D collider)
{
   // if (collider.gameObject.name == "RobotBoy")
   //     pickUpCount++;
    Debug.Log("PickUp " + pickUpCount);
}

查看 Unity API 文档

这篇关于Unity - OnTriggerEnter 未注册碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆