[SOLVED] How can I add an entity child to another entity with code?

I need put a plane in a Entity when i hover that entity and remove it when not.
I know hot to detect when is hover, but not hot to atatch it.

Hi.

Create entity:

    var newEntity = new pc.Entity(); // Create new Entity
    var model = new pc.Model(); // Create model
    var material = new pc.StandardMaterial(); // Create new material
    var node = new pc.GraphNode(); // Create graph node
    
    newEntity.addComponent("model"); // Add model component to the Entity
    model.graph = node; // Set the graph node for model
    
    newEntity.model.model = model; // Set new model to your Entity's model component

Create mesh:


     var plane= new pc.createPlane(this.app.graphicsDevice); // Create geometry
     var meshInstance = new pc.MeshInstance(node, plane, material);  // Set the material
       
     // If you need to place you plane somewhere (like a carpet, huh?)
     //meshInstance.node.setLocalPosition(x,y);
    
     newEntity.model.meshInstances.push(meshInstance); // Add your meshInstance to model

And then just add this Entity to your root element:

this.app.root.addChild(newEntity);

That’s a bit complicated! You can just do something like

var plane = new pc.Entity();
plane.addComponent('model', {type: 'plane'});
// might wanna rotate it to look at the camera
plane.setLocalEulerAngles(90, 0, 0);

// to add the plane to an existing entity
existingEntity.addChild(plane);
1 Like

Yes, you’re right, I totally forgot about that, sorry.