The problem is related to the ‘raycaster.js’ and ‘pickable.js’ scripts.
I am not sure why, but I appear to be unable to pick the objects in the scene with the collision method.
Not sure if I cannot do that dynamically the way I am doing, but the components seem to initialize just fine, just that the raycaster console.log is printing null and confusing me quite a bit. I have tested this with manually added components and that does not work either so im not entirely sure what I am missing.
I think the issue in this case is even though you are adding the required collision component for the raycaster to find it, its size either isn’t correct (box collider) or you haven’t referenced the right mesh for mesh colliders.
Try adding the right collider in editor for start and it will work:
I think this is due to some race condition there the physics is not initialised yet?
Changing it to the following seems to work which is odd:
var Pickable = pc.createScript('pickable');
Pickable.prototype.initialize = function() {
//if there is no rigidbody, add one
if(!this.entity.rigidbody) {
this.entity.addComponent('rigidbody');
}
//if there is no collider, add one
if(!this.entity.collision) {
this.entity.addComponent('collision', {
type: 'mesh',
asset: this.entity.model.asset
});
}
};
Pickable.prototype.interact = function() {
this.entity.destroy();
};
Interesting, I was unaware initializations can have added properties like this.
That should work just fine for this case, though could cause issues for different usecases
It looks like there is a bug here and worth adding a ticket to the GitHub repo with a reproducible. Did a quick investigation and it looks like it doesn’t handle using a model resource directly well on initialisation.
There do appear to be some weird shenanigans when you add the collider like this.
The size of the mesh collider appears not to be the same as the visible mesh
(using the same mesh asset for both collision and rendering ofcourse)
This does work properly if they have been manually added into the editor scene.
Edit: It appears that I am blind. I had added the type and asset parameters to the rigidbody…
Yes, I added the mesh reference on the rigid body instead of on the collider. Which added the standard box collider instead of the mesh I was providing.
Damn you javascript and lack of error handling! Nah, but rly this is my own oversight… Haha