为什么 OnCollisionEnter 没有被触发? [英] Why does OnCollisionEnter not get triggered?

查看:207
本文介绍了为什么 OnCollisionEnter 没有被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象:

对象 A(SteamVR 摄像机):

Object A (SteamVR Camera):

  • 具有启用了Is Kinematic"的刚体.
  • 有一个启用了Is Trigger"的 Box Collider.
  • 具有带有OnCollisionEnter"方法的脚本.

对象 B(地球模型):

Object B (Globe model):

  • 有一个启用了Is Trigger"的球体碰撞器.

我确保碰撞器范围正确,但不知何故 OnCollisionEnter 方法没有被触发.

I made sure that the collider ranges are correctly, but somehow the OnCollisionEnter method does not get triggered.

我用于 OnCollisionEnter 的代码如下所示:

The code that I am using for OnCollisionEnter looks like below:

private void OnCollisionEnter(Collision collision)
{
    Debug.Log("Collision entered");
}

有人能给我一些见解/建议吗?

Could someone provide me some insight/advice?

谢谢转发.

推荐答案

问题:任何对象与触发器之间都没有碰撞

要了解碰撞的工作原理,您首先需要确定哪些对象正在发生碰撞,因为很少有对象会触发 OnCollisionEnter.

根据您的描述,我们可以识别出两种类型的物体.

From your description we can identify 2 types of objects.

对象 A:运动刚体触发器对撞机
对象 B:静态触发碰撞器

Object A: Kinematic Rigidbody Trigger Collider
Object B: Static Trigger Collider

如果您检查碰撞矩阵,则任何具有触发器(对象 A 和对象 B),因此 OnCollisionEnter 不会触发.
我再说一遍:无论其他对象是什么,触发器都不会与任何东西发生碰撞.

If you check the Collision Matrix there could be no collision with anything that has a Trigger (both Object A and Object B), so OnCollisionEnter won't fire.
I repeat: a Trigger does not collide with anything no matter what the other object is.

如果您想从与触发器对象(或其中 2 个,就像您的情况)的碰撞中获取事件,您可以使用 OnTriggerEnter.

If you want to get the event from a collision with a trigger objects (or 2 of them, like in your case) you can use OnTriggerEnter.

考虑在这种情况下,该方法的参数将是 Collider other,即 Collider 而不是 Collision,但在你的如果我认为你根本不需要碰撞.

Consider that in this case the parameter of the method will be Collider other that is a Collider instead of a Collision, but in your case I think you won't need the collision at all.

private void OnTriggerEnter(Collider other) 
    => Debug.Log("entered");

<小时>

关于 Unity 冲突的见解

碰撞取决于刚体和碰撞器设置对象.我可以根据你的情况举几个例子.


INSIGHTS about Unity collisions

The collisions depend on the rigidbody and collider setup objects. I can show a few examples based on your situation.

要考虑的第一个元素是静态碰撞器不是通常设置为静态的对象(这是下面评论中混淆的根源).

The first element to consider is that Static Collider is not an usual Object set as Static (this was a source of confusion in the comments below).

来自 Unity 手册

静态碰撞器
这是一个具有碰撞器但没有刚体的游戏对象.静止的碰撞器用于始终保持不变的关卡几何体位置,永远不会四处走动.传入的刚体对象将发生碰撞使用静态碰撞器但不会移动它.

STATIC COLLIDER
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.

还要考虑您要使用触发器(使您的 STATIC COLLDER 成为 STATIC TRIGGER COLLIDER).

Consider also that you want to use a trigger (that makes your STATIC COLLDER into a STATIC TRIGGER COLLIDER).

基本上触发器背后的想法是:当您想捕捉事件但不希望游戏对象与物理力交互时,请使用触发器."

Basically the idea behind trigger is: "Use triggers when you want to catch the event but you don't want the gameobject interacting with Physics forces."

脚本系统可以检测何时发生冲突并启动使用 OnCollisionEnter 函数的操作.但是,您也可以使用物理引擎只是为了检测一个对撞机何时进入空间另一个不产生碰撞.一个对撞机配置为触发器(使用 Is Trigger 属性)不作为实体对象,只会允许其他碰撞器通过.当一个collider 进入它的空间,触发器将调用 OnTriggerEnter触发器对象脚本上的函数

The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts

这篇关于为什么 OnCollisionEnter 没有被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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