I’ve been trying to add collision to my models in a previous topic, but I’m creating a new one as this is more specific.
So I am trying to add mesh collision components to my models through code using this snipped:
entity.addComponent('collision', {
type: "mesh",
model: this.entity.model.model,
asset: this.entity.model.asset
});
This works on a model (without collision component) placed in the editor, but not when I have loaded the model from web:
this.app.assets.loadFromUrl(url, "model", function (err, asset) {
if(err){ console.log(err); return;}
var entity = new pc.Entity();
entity.addComponent("model");
entity.model.model = asset.resources[0];
var collider = entity.addComponent('collision', {
type: 'mesh',
model: entity.model.model,
asset: entity.model.asset
});
console.log("added col", collider);
self.app.root.addChild(entity);
});
Logging this collision component shows that both the asset and model properties are null.
Project: https://playcanvas.com/project/672760/overview/collision-error
test.js
prints a collision component added in editor
test2.js
prints a collision component added through code
raycast.js
adds cube from web and adds collision which doesn’t work