How to handle mass cloning of objects when "deep clones" are required?

I have an enemy entity that I wanna clone lots of times (~100) and set each clone to a different position.
When i change the position of 1 clone all the other clones get the same position, since cloning does not “deep clone” components.

I also have self written scripts on the enemy.

I’m kinda lost, any ideas?

Hey! According to clone documentation, clone() creates deep copy of an entity.
Can you share your code of cloning so that it may get easy to understand the issue?

https://playcanvas.com/editor/scene/944722

You can see in EnemySpawner that i spawn 3 enemies (clones) but they all land at basically the same position no matter what values i use.

We cannot use setPosition function for rigid bodies as it will overrided by physics calculations.
Just add this line

dasher.findComponent('rigidbody').entity.rigidbody.teleport(new pc.Vec3(posX,0,posZ));

instead of

dasher.findComponent('rigidbody').entity.setPosition(posX, 0, posZ);

and it will work fine :slight_smile:

Thank you SO MUCH! Spent two hours debugging T.T

1 Like