Preventing Instantiating Within Rigidbody Of Object.

Started a new project and run into a major issue right away.

I’m working on a run and gun 3D game. The player moves very fast and can shoot. When an enemy dies, I play a short particle effect, destroy the original enemy object and instantiate a new model of broken pieces of the original in the same position.

The problem arises when the player is moving at full speed and shooting an enemy extremely close. The broken parts are instantiated inside the rigidbody of the player object. When this happens some parts get stuck inside the player and others start to throw null references or Nans for position vectors.

I see two options but don’t care for both. I either instantiate at the nearest position outside of the player bounds, or I delay the instantiate until the player is no longer in that position. I can see edge case issues with both.

This sounds like a scenario that could happen in many games and was wondering how others handle this issue. Are there other options?

The broken parts should not be set to collide with the player, or at least, not at first (you could enable them after a delay or when the player is a certain distance away, if you really want the player to be able to kick them around later).

Hey thanks Joe,

I looked into instantiating with a disabled collider, but the game is fairly frenetic in the heat of play and I need the pieces to interact with each other and everything else but the player collider.

I see I have two options; I can use Physics.IgnoreCollision or traverse layers and fiddle with the collision matrix. I’m going to try ignorecollision as it is a single line in script to implement. I don’t see any way to know if ignorecollision is true or false so I assume I need to set my own flag to re-enable it. Calling ignorecollision = false repeatedly seems like a bad idea.

I’ve never used it, but Physics.IgnoreCollision looks like it’s perfect for this sort of use case. Please let us know how it goes!

Today seems to be bazar day for me…

I actually have not gotten to implementing the ignore collision code yet. I decided to do a little cleanup and refactoring before I add. I commented out a lot of old code and after reworking the only thing I didn’t add back in were the sound effects. After some testing I was unable to produce the errors. I don’t think it was the refactoring that solved the problem. I removed and re-established the audioclips from scratch and replaced. So far I’m not able to reproduce the error. One of the errors I was getting was a null reference to an audioclip, so maybe that was the issue all along???

This error was hard to reproduce from the beginning. It only seemed to happen sporadically when running and shooting an enemy at extreme close range. It seemed to only happen when there were a large number of enemies or enemy parts and things are very chaotic. This makes it extremely hard to troubleshoot as a lot is happening in a very short time frame. I’ll need to do some rigorous testing to be sure it is truly solved, but for now I’m thinking this is no longer an issue and leaving me scratching my head over why an audioclip caused the problem.

Still haven’t implemented the IgnoreCollision and don’t think I need to now. No the problem was not an audioclip and eventually I ran into the error again.

Digging deeper I found I was not destroying the UI element I thought I was. Seems I as destroying the image script on the UI element and leaving an empty RectTransform. Visually things looked fine. I also found the first element in my array of prefabs causes a problem. When being destroyed, It is visually gone but the transform, rigidbody, and collider remained. I can’t see what changed to make it not visible in the inspector. Thinking the model was bad I used one of the known good models in it’s place and I still get errors on the first prefab in the array of prefabs. Skipping the first prefab seems to have solved this. I said that before, so I need to thoroughly take this through the paces before I declare a victory. At least all items are now being created and destroyed properly (I hope!).

1 Like

Can you just use layers & set the player not to interact with the dead body in the physics settings?