为什么OnCollisionEnter被调用两次? [英] Why is OnCollisionEnter getting called twice?

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

问题描述

我的场景中有一个 Cube (玩家)和一个 Planet (地板).两者都附有Colliders,并且只有 Cube 附有RigidBody组件. 立方体位于平面上方.

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. Cube is positioned above the Plane.

多维数据集附带有一个脚本,用于查找冲突.

Cube has a script attached to it which looks for Collisions.

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

问题:

当飞机与立方体碰撞时,该消息被打印两次.

When the Plane and the Cube collides, the message gets printed twice.

...........................碰撞

...........................Collision

UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision)(位于Assets/jump.cs:34)

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

...........................碰撞

...........................Collision

UnityEngine.Debug:Log(Object) jump:OnCollisionEnter(Collision)(位于Assets/jump.cs:34)

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

为什么会这样?碰撞发生了两次吗?

Why is this happening? Is collision happening twice?

推荐答案

首先,两个日志似乎都来自 34 行的 jump.cs 脚本.因此我们可以肯定地说问题出在一个脚本上,而不是很多脚本上.

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.

为什么会这样?

Why is this happening?

以下是可能的原因:

1 .您具有OnCollisionEnter功能的脚本已附加到多个GameObjects.那些多个游戏对象是彼此碰撞的两个游戏对象.当它们碰撞时,OnCollisionEnter被多次调用.

1.Your script with the OnCollisionEnter function is attached to multiple GameObjects. Those multiple GameObject's are the two GameObject's that are colliding with one another. When they collide, OnCollisionEnter is called multiple times.

2 .具有OnCollisionEnter功能的脚本会多次附加到相同的GameObject.

2.Your script with the OnCollisionEnter function is attached to the-same GameObject multiple times.

3 .两个碰撞游戏对象都在碰撞,退出然后再次碰撞.这是可能的,但在这种情况下不太可能,但是值得一提.

3.Both Colliding GameObjects are colliding, exiting then colliding again. This is possible but not likely in this case but it is worth mentioning it.

您也可以通过添加OnCollisionExit进行验证:

You can verify this by adding OnCollisionExit too:

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

然后使用OnCollisionEnter功能检查日志的打印顺序.

Then check which order the logs are printed with the OnCollisionEnter function.

4 .您当前正在碰撞的每个GameObject上有多个碰撞器.如果需要将多个对撞机用作复合对撞机,请将其添加到空的GameObject子级中,然后将它们放在不同的标签中,以便可以从脚本中忽略它们.

4.You have more than one colliders on each GameObject that are currently colliding. 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.

替换:

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

使用

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

如果两个日志中的GameObjects名称相同,则是,它可能发生两次.

If the name of the GameObjects in both log are the-same then yes, it is likely happening twice.

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

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