简体   繁体   English

Unity 3D Collisiondetection

[英]Unity 3D Collisiondetection

I generate an Gameobjct array of speheres and try to apply a collider to them to detect collision with my character controller. 我生成一个Gameobjct speheres数组,并尝试对它们应用一个碰撞器来检测与我的角色控制器的碰撞。

i tried serverel methods, nothing worked. 我尝试了serverel方法,没有任何效果。 Why Unity doent detect the collision ? 为什么Unity会发现碰撞?

Script for generating the array : 生成数组的脚本:

 public GameObject[] Chunkzufall(float l, float b, int n)
{
    GameObject chunk = new GameObject();
    GameObject[] chunks = new GameObject[n];

    chunkSpeed = new float[n];
    chunkMaxH = new float[n];
    chunkMinH = new float[n];

    for (int j = 0; j < n; j++)
    {
        float h = 1;
        float posX = Random.Range(0.0f, b);
        float posZ = Random.Range(0.0f, l);

        GameObject group = Chunk(h);
        group.transform.Translate(posX, 0.0f, posZ);

        group.transform.parent = chunk.transform;
        chunk.tag = "reset";
        chunk.name = "chunk";

        chunkSpeed[j] = (float) Random.Range(-0.04f, 0.04f);
        chunkMaxH[j] = (float) Random.Range(2.0f, 10.0f);
        chunkMinH[j] = (float) Random.Range(-2.0f, -10.0f);

        chunk.AddComponent<SphereCollider>();
        chunk.GetComponent<SphereCollider>().isTrigger = true;
        chunk.GetComponent<SphereCollider>().radius = 5.0f;

        chunks[j] = chunk;

    }
    return chunks;
}

public void MoveChunks(GameObject[] chunks)
{
    int i = 0;
    foreach(GameObject chunk in chunks)
    {
        Vector3 position = chunk.transform.GetChild(i).position;

        if (position.y >= chunkMaxH[i] || position.y <= chunkMinH[i])
        {
            chunkSpeed[i] = chunkSpeed[i] * -1;
        }

        position.y = position.y - chunkSpeed[i];
        chunk.transform.GetChild(i).position = position;
        chunk.GetComponent<SphereCollider>().center = position;

        i++;
    }
    i = 0;
}

collision trigger function : 碰撞触发功能:

    private void OnTriggerEnter(Collider col) {
    if(col.gameObject.tag == "reset") {
        transform.position = new Vector3(startX, startY, startZ);
        Debug.Log("chunk");
    }
}

To detect a trigger on the dynaimcally created object, you have to enable the IsTrigger flag, add collider to the object. 要在动态创建的对象上检测触发器,必须启用IsTrigger标志,向对象添加对撞机。 The object must also have Rigidbody attached to it. 该对象还必须附加Rigidbody It looks like you already have the IsTrigger flag and a collider but you are missing Rigidbody . 看起来你已经有了IsTrigger标志和一个对撞机但是你缺少Rigidbody

Replace this: 替换这个:

chunk.AddComponent<SphereCollider>();
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;

with

chunk.AddComponent<SphereCollider>();
chunk.AddComponent<Rigidbody>(); //ADDS RIGIDBODY COMPONENT
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;

Do you have Rigidbody attached? 你有Rigidbody吗? The objects need Rigidbody attached to it to be able to detect collision. 物体需要连接Rigidbody才能检测到碰撞。 And do note that for each pair of collider and collided objects, having one of them with Rigidbody attached is enough to capture the event. 请注意,对于每对碰撞对象和碰撞对象,其中一个与Rigidbody连接就足以捕获事件。 In your case, if you don't want to process the collision betweens spheres, then attach Rigidbody to your character object is good enough. 在您的情况下,如果您不想处理两个球体之间的碰撞,那么将Rigidbody附加到您的角色对象就足够了。

You could add a RigidBody to all of the spheres but that would be very inefficient if you want to have many of them. 你可以在所有的球体上添加一个RigidBody ,但是如果你想拥有很多球体,这将是非常低效的。 It is enough to have 1 RigidBody for every 2 objects that you want to check collision against. 对于要检查碰撞的每2个对象,就足以拥有1个RigidBody

So in your case it would be enough to put a RigidBody component on your character controller object. 因此,在您的情况下,将RigidBody组件放在角色控制器对象上就足够了。

Also, make sure the RigidBody is not set to Kinematic. 另外,请确保RigidBody未设置为Kinematic。

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

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