简体   繁体   English

为什么 OnCollisionEnter 被调用两次?

[英]Why is OnCollisionEnter getting called twice?

I have got a Cube (player) and a Plane (floor) in my Scene.我的场景中有一个立方体(播放器)和一个平面(地板)。 Both have Colliders attached to them and there is a RigidBody component attached only to the Cube .两者都附加了Colliders并且有一个RigidBody组件仅附加到Cube Cube is positioned above the Plane . Cube位于Plane上方。

Cube has a script attached to it which looks for Collisions. Cube附有一个脚本,用于查找碰撞。

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

PROBLEM:问题:

When the Plane and the Cube collides, the message gets printed twice.当平面和立方体碰撞时,消息会打印两次。

...........................Collision ...........................碰撞

UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision) (at Assets/jump.cs:34) UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision)(在 Assets/jump.cs:34)

...........................Collision ...........................碰撞

UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision) (at Assets/jump.cs:34) UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision)(在 Assets/jump.cs:34)

Why is this happening?为什么会这样? Is collision happening twice?碰撞是否发生两次?

First of all, both logs seems to be coming from the jump.cs script at line 34 so we can safely say that the problem is just from one script not from many scripts.首先,两个日志似乎都来自第34行的jump.cs脚本,所以我们可以有jump.cs地说问题只是来自一个脚本而不是许多脚本。

Why is this happening?为什么会这样?

Here are the possible reasons:以下是可能的原因:

1 .Your script with the OnCollisionEnter function is attached to multiple GameObjects. 1 .带有OnCollisionEnter函数的脚本附加到多个游戏对象。 Those multiple GameObject's are the two GameObject's that are colliding with one another.那些多个游戏对象是相互碰撞的两个游戏对象。 When they collide, OnCollisionEnter is called multiple times.当它们发生碰撞时, OnCollisionEnter会被多次调用。

在此处输入图片说明

2 .Your script with the OnCollisionEnter function is attached to the-same GameObject multiple times. 2 .带有OnCollisionEnter函数的脚本OnCollisionEnter附加到同一个游戏对象。

在此处输入图片说明

3 .Both Colliding GameObjects are colliding, exiting then colliding again. 3 .Both Colliding GameObjects 正在碰撞,退出然后再次碰撞。 This is possible but not likely in this case but it is worth mentioning it.这是可能的,但在这种情况下不太可能,但值得一提。

You can verify this by adding OnCollisionExit too:您也可以通过添加OnCollisionExit来验证这一点:

void OnCollisionExit(Collision collisionInfo) 
{
        print("Collision Out: " + gameObject.name);
}

Then check which order the logs are printed with the OnCollisionEnter function.然后使用OnCollisionEnter函数检查日志的打印顺序。

4 .You have more than one colliders on each GameObject that are currently colliding. 4 . 当前正在发生碰撞的每个游戏对象上都有多个碰撞器。 If you need to use multiple colliders as a compound collider, add them to a child empty GameObject then put them in a different tag so that you can ignore them from script.如果您需要使用多个对撞机作为复合对撞机,请将它们添加到子空游戏对象中,然后将它们放在不同的标签中,以便您可以从脚本中忽略它们。

在此处输入图片说明


Is collision happening twice?碰撞是否发生两次?

Only you can answer this since the scene is in front of you.只有你能回答这个问题,因为场景就在你面前。

Replace:代替:

Debug.Log("...........................Collision");

with

Debug.Log("Collision: " + gameObject.name);

If the name of the GameObjects in both log are the-same then yes, it is likely happening twice.如果两个日志中游戏对象的名称相同,那么是的,它可能会发生两次。

I think it might be about the mesh collider.我认为这可能与网格对撞机有关。 I had the same problem and when I changed the mesh collider to a box collider, the problem disappeared.我遇到了同样的问题,当我将网格对撞机更改为盒式对撞机时,问题消失了。 My guess for the cause of this is the multiple collision lines within the mesh collider.我对造成这种情况的原因的猜测是网格碰撞器中的多条碰撞线。 Give it a try.试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM