Issue adding collider at runtime

if you are sure thats not the problem i will look and see if i cant find out what happening but this what should be happening:
Screenshot of what your code should output

@GaLzZy
Looks like you have to set the localScale of the entity before adding the collision component. My guess is when it creates the physical shape from the mesh in the collision component, it scales the shape based on the localScale of the entity at the time.

As a warning, this does mean you can’t scale the entity at runtime and have the collision scale with it without forcing the collision component to recreate the physical shape.

Fixed code: https://playcanvas.com/editor/scene/734920

//Creates a new cube when button is clicked
Ui.prototype.createButtonEvent = function(create_button) {
    var self = this;
    
    self.create_button.addEventListener('click', function() {
            var entity = new pc.Entity();
            entity.addComponent('model', {
                type: 'asset',
            });
            entity.model.asset = self.houses[0];
            entity.model.castShadows = false;
            entity.model.receiveShadows = false;
        
            entity.setLocalScale(0.2, 0.2, 0.2);
        
            entity.addComponent('collision', {
                type: 'mesh',
            });
            entity.collision.model = self.houses[0].resource;
            entity.name = 'entity' + self.gm.entityId;
            entity.setLocalPosition(
                Math.floor(pc.math.random(0, 5)),
                0,
                Math.floor(pc.math.random(0, 5))
            );
            self.gm.createEntity(entity);
        }, false);
};
2 Likes

YOU ARE A GENIUS ! Why didn’t I think of this lol ! Thanks a lot my man appreciate your help :smiley:

As a side note, you can also clone existing entities which would save you having to write the code to build an entity from scratch.

1 Like

Like you mean putting the entire entity as an attribute ? I tried that :open_mouth:

See: https://playcanvas.com/editor/scene/734920 for new code that clones the entity referenced in the editor (still in ui.js).

Ohhh I see yea that’s way better i’m gonna do that thanks !

New question just came up, if I use the same material file for all the entities of houses, when I change the base color of the mat, will it change it to all houses or the .clone() method can make it so it is a new one everytime ? Because now, I need to change the base color of the houses…

Thanks.

It will use the same material. After cloning the entity, you could go through all the model components and clone the material.

Ok and now, if I wanna change my mat base color i’m doing this, but it doesn’t seem to be working : myEntity.model._model.meshInstances[0]._material._diffuse.fromString(’#FF0000’);

It only works when I just created the entity and not when I grab it from app.root.findByName()…