I’m losing my mind over this bug I’ve encountered.
When I haven’t killed/disposed an enemy mesh in my scene the collision detection/pick up works perfectly on the health pack and the energy pack I have placed in my scene. The code for enemy kill is this and it’s called when the enemy health reaches 0:
The collision detection between the player-camera and the objects in my scene is handled by this code :
And this is how they look in the scene description:
healthpack being a cube that acts like a collision mesh and the healthpackmesh that acts like the actual model.
When I have killed an enemy in the scene I can pick the health pack but not the energy pack. It detects the collision the sense that the camera stops moving when I go towards it but it doesn’t actually remove it nor it prints the name of it via the console.log call in the above picture.
It’s very hard/impossible to actually provide a playground due to the way my scene is structured.
It’s been a long time since I encountered this problem and I have no idea where to look so ANY suggestions would be soooo appreciated.
Hi @HappyDev
To be sure that I understand correctly:
you have 1 health pack and 1 energy pack in your scene that you can pick up
after you killed an enemy, you can pick that exact same health pack but not the energy pack
is that correct?
What does happen when you have multiple health pack and energy pack? Can you only pick up health pack?
The energy and health pack only differ by the name? I mean, it’s the same Collectible class instanced for both types?
Can you show me the code that creates the Collectible impostor?
Yes currently I have 1 health pack and 1 energy pack that I can pick up
Yes, when I’ve killed an enemy I can’t pickup the energy pack
What does happen when you have multiple health pack and energy pack? Can you only pick up health pack?
Just tested putting another healthpack in the scene and it turns out when I kill an enemy now I can’t pick up ANYTHING.
The energy and health pack only differ by the name? I mean, it’s the same Collectible class instanced for both types?
Yes the same collectible class instanced
Can you show me the code that creates the Collectible impostor?
I removed that but , I created with blender a cube mesh that acted as the collision mesh and parent of the health pack / energy pack model. I removed it in my scene and it doesn’t make a difference I just enable the checkCollisions property since the models are low poly.
Thank you so much for doing this, I know the situation is tight cause of no playground use. I’m gonna setup my github right now and maybe the project can be checked as a whole. I suspect it has to do with the way I load the models or the way I use the registerBeforeRender methods.
Ι use the cannon impostors only for these specific bullets.
Some warned me that there might be some conflicts with the normal collisions but apparently making the mass of the camera impostor 0 doesn’t affect anything. In addition disabling/commenting out any impostor/cannon reference in my code still doesn’t solve anything, so I don’t think that’s the problem.
Right now I just use the checkCollisions on the actual mesh.
Ι get what you are saying but I disabled every cannon/impostor reference in my code, since it was just an experiment with projectile based attacks which I can get by removing if it makes the normal collisions work again, and the problem still exists.
Can’t this be done with the regular collision check ? I mean it’s working alright BEFORE I destroy an enemy.
I think the issue is in the destruction of the enemy. When it’s killed, don’t call the destructor of the enemy and see if it works (move the enemy far away for example).
Otherwise, it can be the spawning of the collectible. To test that in a second phase, bind a key to spawn a collectible randomly in the world and see if each one is working.
There might be a bug in Babylon but we have to understand what’s going on before doing anything.
So, you removed the dispose and also nothing is done in the addcollectible and you have the same issue?
Do you think you can copy and paste your code in a playground?
The idea is to remove the maximum of code to get the bad behavior so it’s easier to understand.
Upon clicking on the right you enter the game and your mouse is captured.
I haven’t put a cross hair right now cause I don’t know how to do it but a ray is coming from the camera so when you click a mesh it is destroyed.
Everything is working great apparently but there are so many things missing from this. Like the classes and everything.
All this makes me feel so bad cause it means that what I’m trying to do generally works but my code is so shitty that I can’t even find what’s causing it and I’m too bad at this to even know where to look and I’ve, with your help in the previous replies of course, exhausted my options.
The only option that remains is to rewrite everything and I don’t know if I can handle it.
Maybe it’s my bad usage of classes or events or whatever.
You were a total bro along the way though, helpful to the bone.
[EDIT] In general though, something is broken with the way the enemies/meshes are disposed. Cause I tried many things and dispose directly the meshes with no use of their class methods and still the same problem.
And it doesn’t have to do anything with the registerBeforeRender additions cause I’ve tried removing their usage completely and still get the same problem.
[EDIT2]
Using
mesh.setEnabled(false)
instead of
mesh.dispose()
Seems to fix the problem but probably that isn’t right but has a similar effect of making the mesh “disappear” from the scene. Really weird the way dispose() causes the problem no matter how I call it, directly on the mesh from wherever or indirectly through its class method.
Also please warn me if there are any dangers of using the setEnabled(false) method as a bandaid.