Good morning everyone
i’ve found myself lost again regarding clone and teleport functions, as you can see on the project i have a beautiful black cube which i want to clone and move to the initial location when the space bar is pressed.
But somehow it’s not working properly … what i’m missing?
Press R to reset the Cube - Press Space to generate a copy
EDIT: I’ve implemented another function to reset the cube position using translate and it’s working, but whenever you use the clone function everything stops working properly. I think i’m missing something between the reference from clone and the original.
EDIT2: Keep pressing R and start pressing Space, the start location is being reduced by the Y axis somehow
In a nutshell, you need to add the clone object to the scene graph:
CloneCube.prototype.cloneLordCube = function(){
console.log("Clonning the actual cube from: "+this.entity.getPosition());
var lordCubeClone = this.entity.clone();
// Add the clone to the scene
this.entity.parent.addChild(lordCubeClone);
console.log("Clonned at "+lordCubeClone.getPosition());
lordCubeClone.rigidbody.teleport(this.startPosition);
}
There is another issue that I had to workaround where the update for the cloned entity was called in the same frame as the clone was created so it would also create a clone as soon as it was created. Then those clones would do the same, etc.
So I added a flag to ignore input on the first frame update. You also need to store a copy of the start position as getPosition() returns a reference and not a copy of a pc.Vec3.
I realized what was happening when i saw the clones calling themselves as the original cube instance. So, i set the flag but i felt that was not correct because i didn’t want the clones to Reset Or Spawn a new clone. So i just disabled the script of the clone and everything went smooth.
(Also, i cloned the initial getPosition)
Thank you so much for your answer
(If you saw something that i did not mention, please, leave a line, i will appreciate it)
In which case rather than putting the logic of cloning on the cube, you have the script in a different object (such as the root) so that the cloned object(s) don’t have scripts/logic that they don’t need/use.