[SOLVED] How to add collision and rigibody to a cloned entity

I try to create some basic Minecraft-like game.
So I need some procedural terrain generation.
At the moment, I don’t use a noise, but I clone an entity already made multiple times.
The thing is that both collision and rigidbody are not cloned.
How can I clone them, or add them later in the code ?

My code :

    var cloned = pc.app.root.findByName("Grass_Block");
    for (var rows = -10; rows <= 10; rows++) {
        for (var columns = -10; columns <= 10; columns++) {
            var e = cloned.clone(); // Clone Entity
            pc.app.root.addChild(e);
            height = Math.random();
            if (height < 0.85) {
                height = 0.5;
            } else {
                height = 1.5;
            }
            e.setPosition(rows, height, columns);
        }
    }

Thanks by advance !

Hi @TheAssassin71,

How are you preparing your cloned reference entity?

Because if it includes a collision and rigidbody component, the clone() method will make sure to make a deep copy and include those components to the new entity.

Well, I create it not using a script, with the editor.
Like so :

I see, the issue is most likely with your static rigidbody component. From the moment it’s created and added to the physics simulation you can’t further move it.

You can solve this by changing the order of your calls, I think the following will work:

    var cloned = pc.app.root.findByName("Grass_Block");
    cloned.enabled = false;

    for (var rows = -10; rows <= 10; rows++) {
        for (var columns = -10; columns <= 10; columns++) {
            var e = cloned.clone(); // Clone Entity

            height = Math.random();
            if (height < 0.85) {
                height = 0.5;
            } else {
                height = 1.5;
            }
            e.setPosition(rows, height, columns);

            pc.app.root.addChild(e);
            e.enabled = true;
        }
    }
3 Likes

Yay it works perfectlt fine !

Thank you really !

1 Like