[SOLVED] Block Creation Distance Becoming Exponentially Greater

Hello, and thanks for the help!

I am trying to make a minecraft-esque like building system, where you click on blocks and new ones are made. I have a raycast system that calls the following function,

EntityCreator.prototype.spawnCube = function(){
    //Initial Parent Block Init
    eval("var block" + i + " = new pc.Entity();");
    eval("block" + i + ".name = 'block" + i + "';");

    var afterBlockPosX = blockPosX + 7;
    var afterBlockPosY = blockPosY - 4.5;
    var afterBlockPosZ = blockPosZ;

    eval("block" + i + ".setLocalPosition(afterBlockPosX, afterBlockPosY, afterBlockPosZ);");

    eval("this.app.root.addChild(block" + i + ");");

//under here the sides of the block with the actual collisions and visuals are added

where blockPosX, Y, and Z are the position of the entity that the raycast hit (which would be one of the sides of the block). I tried this out and ran across a weird offset so I added the afterBlockPos variables to fix it. However, then I tried to click on the blocks that where created and the offset effect seems to become exponentially more of a problem. This also applies without the offset I added, the offset that was already there getting worse instead.

I have been trying to fix the offset and am stumped. Any help would be greatly appreciated. Below is the project link if needed.
https://playcanvas.com/editor/project/1091602

Thanks again, Nathan

Hi @Nathan_Crouch,

I’m not sure about the offset, but before that may I ask why are you using eval()?

Thanks for the reply @Leonidas
I know it is poor practice but I am using it to make sure the new boxes created have different names. I dont actually know if this is necessary and if it is not I would love to remove it.

I fixed the problem. It turns out I was setting the position of the sides of the cube with an offset that was the same that positioned the cube…

Ex: parent cube is set to 1, 1, 1
sides are set to 1 + 1, 1 + 2, 1 + 3
When the cube is parented to the sides :joy:

1 Like