简体   繁体   English

如何为所有同级游戏对象禁用碰撞?

[英]How can I disable collisions for all sibling game objects?

I have a spawner which spawns balloons every second. 我有一个生成器,每秒生成一个气球。 I want to stop these balloons from colliding with other balloons. 我想阻止这些气球与其他气球碰撞。 I can see how you can loop through child objects from a parent, but I don't know how to loop through other child objects that share the same parent. 我可以看到如何从父级循环遍历子对象,但是我不知道如何在共享同一父级的其他子级对象之间循环。 All balloons are spawned in the same parent. 所有气球都在同一父对象中生成。

Currently I have this but it obviously doesn't work. 目前我有这个,但显然不起作用。 I tried transform.parent.transform too but this didn't work either. 我也尝试了transform.parent.transform但这也不起作用。 It only spawns one balloon and the script breaks for "object not set to an instance of an object" on the same line. 它仅产生一个气球,并且脚本在同一行上中断“对象未设置为对象的实例”。

var NewBalloon = Instantiate(balloons[0], transform.position, Quaternion.Euler(new Vector3(-90, 0, 0)));
NewBalloon.transform.parent = GameObject.Find("Balloons").transform;

foreach(Transform child in transform.parent)
{
      Physics.IgnoreCollision(NewBalloon.GetComponent<Collider>(), child.GetComponent<Collider>());
}

Not quite the way I intended to do it, but I found that if you go to "Edit" > "Project Settings" > "Physics" (on version 5.5), I could disable the checkbox for balloons / balloons which stops them colliding with each other. 并非完全按照我的意图进行,但是我发现,如果转到"Edit" > "Project Settings" > "Physics" (在5.5版上),则可以禁用气球/气球的复选框,以阻止它们碰撞彼此。

Edit: This seems to be the best way to do it. 编辑:这似乎是最好的方法。 No need to add scripting for it, and it just works perfectly. 无需为其添加脚本,它运行完美。

but I don't know how to loop through other child objects that share the same parent. 但我不知道如何遍历共享同一父对象的其他子对象。

You don't have to! 不用了!

This is really easy. 这真的很容易。 Just create a layer and put the balloon prefab in the layer. 只需创建一个图层,然后将气球预制件放到该图层中即可。 Let's say Layer 9. 假设是第9层。

Now, run this in the Awake function to ignore collison between each balloon in that layer: 现在,在Awake函数中运行此命令,以忽略该层中每个气球之间的碰撞:

Physics.IgnoreLayerCollision(9, 9, true);

If you want to recognize collison, run this: 如果要识别Collison,请运行以下命令:

Physics.IgnoreLayerCollision(9, 9, false);

That's it. 而已。

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

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